【POJ 2387 --- Til the Cows Come Home】dijkstra || dijkstra堆优化 || SPFA

mac2026-03-26  5

【POJ 2387 --- Til the Cows Come Home】dijkstra || dijkstra堆优化 || SPFA

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

解题思路

最短路径水题。

AC代码1(dijkstra):

#include <iostream> #include <algorithm> using namespace std; #define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define inf 0x3f3f3f3f const int MAXN = 2010; int n,m,cost[MAXN][MAXN],dis[MAXN]; bool vis[MAXN]; void dijkstra() { fill(dis,dis+MAXN,inf); fill(vis,vis+MAXN,false); dis[n]=0; while(true) { int v=-1; for(int i=1;i<=n;i++) if(!vis[i] && (v==-1 || dis[i]<dis[v])) v=i; if(v==-1) break; vis[v]=true; for(int i=1;i<=n;i++) dis[i]=min(dis[i],dis[v]+cost[v][i]); } } int main() { SIS; int x,y,z; while(cin >> m >> n) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i==j) cost[i][j]=0; else cost[i][j]=inf; for(int i=0;i<m;i++) { cin >> x >> y >> z; if(cost[x][y]>z) cost[x][y]=cost[y][x]=z; } dijkstra(); cout << dis[1] << endl; } return 0; }

AC代码2(dijkstra堆优化):

#include <iostream> #include <algorithm> #include <queue> using namespace std; #define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define inf 0x3f3f3f3f const int MAXN = 1010; int n,m,cost[MAXN][MAXN],dis[MAXN]; bool vis[MAXN]; void dijkstra() { fill(dis,dis+MAXN,inf); fill(vis,vis+MAXN,false); priority_queue<pair<int,int> > q; q.push(make_pair(0,n)); dis[n]=0; while(!q.empty()) { int x=q.top().second; q.pop(); for(int i=1;i<=n;i++) { if(i!=x && dis[i]>dis[x]+cost[x][i]) { dis[i]=dis[x]+cost[x][i]; q.push(make_pair(-dis[i],i)); } } } } int main() { SIS; while(cin >> m >> n) { int x,y,z; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i==j) cost[i][j]=0; else cost[i][j]=inf; for(int i=0;i<m;i++) { cin >> x >> y >> z; if(cost[x][y]>z) cost[x][y]=cost[y][x]=z; } dijkstra(); cout << dis[1] << endl; } return 0; }

AC代码2(SPFA):

#include <iostream> #include <algorithm> #include <queue> using namespace std; #define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define inf 0x3f3f3f3f const int MAXN = 1010; int n,m,cost[MAXN][MAXN],dis[MAXN]; bool vis[MAXN]; void spfa() { fill(dis,dis+MAXN,inf); fill(vis,vis+MAXN,false); queue<int> q; dis[n]=0; q.push(n); while(!q.empty()) { int x=q.front(); q.pop(); vis[x]=false; if(dis[x]>dis[1]) continue; for(int i=1;i<=n;i++) { if(i!=x && dis[i]>dis[x]+cost[x][i]) { dis[i]=dis[x]+cost[x][i]; if(!vis[i]) q.push(i); } } } } int main() { SIS; while(cin >> m >> n) { int x,y,z; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i==j) cost[i][j]=0; else cost[i][j]=inf; for(int i=0;i<m;i++) { cin >> x >> y >> z; if(cost[x][y]>z) cost[x][y]=cost[y][x]=z; } spfa(); cout << dis[1] << endl; } return 0; }
最新回复(0)