# 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

#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
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<n;i++)
//从剩下的点中寻找出最短距离的点
if(!used[i] && (v==-1 || d[i]<d[v])) v=i;
if(v==-1) break;//如果所有的点都不可以选那么就退出
used[v]=true;
for(int i=0;i<n;i++)//更新所有点的最短距离
d[i]=min(d[i],d[v]+cost[v][i]);
}
return;
}
int main(){
cin >> r >> n;
//注意点之间的花费如果题中没有那么就是INF
for(int i=0;i<maxn;i++)
for(int j=0;j<maxn;j++)
cost[i][j]=INF;
while(r--){
int x,y,val;
scanf("%d%d%d",&x,&y,&val);
x--;y--;
if(cost[x][y]>val){//这个题的坑点就是可能会有重边
cost[x][y]=val;
cost[y][x]=val;
}
}
dijkstra(0);
cout<<d[n-1]<<endl;
return 0;
}


//http://poj.org/problem?id=2387
#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
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<int,int> p;
struct edge{int to,cost;};
int n,r;
vector<edge> E[maxn];
int d[maxn];

void dijkstra(){
fill(d,d+n,INF);
d[0]=0;
priority_queue< p,vector<p>,greater<p> > 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<len;i++){
edge t=E[v][i];
int newdist=t.cost + d[v];
if(newdist < d[t.to]){
d[t.to]=newdist;
que.push(p(d[t.to],t.to));
}
}
}
return;
}
int main(){
cin >> 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;
}

#include <algorithm>
#include <iostream>
#include <utility>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
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<r;i++){
edge t=E[i];
if(d[t.from]!=INF && d[t.to] > 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<r;i++){
int x,y,val;
scanf("%d%d%d",&x,&y,&val);
x--;y--;
E[i]=(edge){x,y,val};
}
Bellman_ford(0);
cout << d[n-1] << endl;
return 0;
}