Til the Cows Come Home(最短路)

原题链接
Til the Cows Come Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 45313 Accepted: 15372
Description

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input

  • Line 1: Two integers: T and N

  • Lines 2…T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1…100.
    Output

  • Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
    Sample Input

5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output

90
Hint

INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.
Source

USACO 2004 November
这个题简直就是一个最短路问题的裸题,问的问题实在是太经典了
下面是用最基本的dijkstra算法来实现的代码,他的复杂度是O(|v|^2)

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;

int r,n;
int cost[maxn][maxn],d[maxn];
bool used[maxn];

void dijkstra(int s){
        fill(d,d+n,INF);
        fill(used,used+n,false);
        d[s]=0;
        while(true){
                int v=-1;
                for(int i=0;i> r >> n;
        //注意点之间的花费如果题中没有那么就是INF
        for(int i=0;ival){//这个题的坑点就是可能会有重边
                        cost[x][y]=val;
                        cost[y][x]=val;
                }
        }
        dijkstra(0);
        cout<

下面是用优先队列优化过的dijkstra算法,他的复杂度是O(|E|log|V| )

//http://poj.org/problem?id=2387
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
const int maxr=2000 + 5;

typedef pair p;
struct edge{int to,cost;};
int n,r;
vector E[maxn];
int d[maxn];

void dijkstra(){
        fill(d,d+n,INF);
        d[0]=0;
        priority_queue< p,vector

,greater

> que; que.push(p(0,0)); while(!que.empty()){ p now=que.top();que.pop(); int dist=now.first,v=now.second; if(dist > d[v]) continue; int len=E[v].size(); for(int i=0;i> r >> n; while(r--){ int x,y,val; scanf("%d%d%d",&x,&y,&val); x--;y--; E[x].push_back( (edge){y,val} ); E[y].push_back( (edge){x,val} ); } dijkstra(); cout << d[n-1] << endl; return 0; }

下面是Bellman_ford算法,他的复杂度是O(|E||V|),他的优势在于可以计算有负边存在的情况,但是前两种算法却不可以

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

typedef long long ll;
const int MOD = int(1e9) + 7;
//int MOD = 99990001;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-9;
const double OO = 1e20;
const double PI = acos(-1.0); //M_PI;
const int fx[] = {-1, 1, 0, 0};
const int fy[] = {0, 0, -1, 1};
const int maxn=1000 + 5;
const int maxr=2000 + 5;
struct edge{int from,to,cost;};
int r,n;
edge E[maxr];
int d[maxn];
void Bellman_ford(int s){
        fill(d,d+n,INF);
        d[s]=0;
        while(true){
                bool update=false;
                for(int i=0;i d[t.from] + t.cost){
                                d[t.to]=d[t.from] + t.cost;
                                update=true;
                        }
                        //这是无向图情况下的添加的更新,如果是有向图就不需要这一步
                        if(d[t.to]!=INF && d[t.from] > d[t.to] + t.cost){
                                d[t.from]=d[t.to] + t.cost;
                                update=true;
                        }
                }
                if(!update) break;
        }
        return;
}
int main(){
        cin >> r >> n;
        for(int i=0;i

你可能感兴趣的:(▶︎算法与数据结构)