怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 33015 | Accepted: 11174 |
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.
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.
* 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
理论上的效率应该是Dijsktra > SPFA > Bellman_Ford,但是前两者我用了vector,影响了效率,导致贝尔曼是最快的,迪杰斯特拉其次。
1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <queue> 5 using namespace std; 6 7 const int SIZE = 1005; 8 const int INF = 0x2fffffff; 9 bool S[SIZE]; 10 int N,D[SIZE]; 11 struct Node 12 { 13 int vec,cost; 14 }; 15 struct comp 16 { 17 bool operator ()(int & a,int & b) 18 { 19 return D[a] > D[b]; 20 } 21 }; 22 vector<Node> G[SIZE]; 23 priority_queue <int,vector<int>,comp> QUE; 24 25 void dijkstra(int); 26 void relax(int,int,int); 27 int main(void) 28 { 29 int t,from; 30 Node temp; 31 32 scanf("%d%d",&t,&N); 33 while(t --) 34 { 35 scanf("%d%d%d",&from,&temp.vec,&temp.cost); 36 G[from].push_back(temp); 37 swap(from,temp.vec); 38 G[from].push_back(temp); 39 } 40 dijkstra(N); 41 printf("%d\n",D[1]); 42 43 return 0; 44 } 45 46 void dijkstra(int s) 47 { 48 fill(D,D + SIZE,INF); 49 D[s] = 0; 50 S[s] = true; 51 QUE.push(s); 52 53 while(!QUE.empty()) 54 { 55 int cur = QUE.top(); 56 int len = G[cur].size(); 57 S[cur] = true; 58 QUE.pop(); 59 for(int i = 0;i < len;i ++) 60 relax(cur,G[cur][i].vec,G[cur][i].cost); 61 if(cur == 1) 62 return ; 63 } 64 } 65 66 void relax(int from,int to,int cost) 67 { 68 if(D[to] > D[from] + cost) 69 { 70 D[to] = D[from] + cost; 71 if(!S[to]) 72 QUE.push(to); 73 } 74 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int INF = 0x5fffffff; 6 const int SIZE = 1005; 7 bool UPDATE; 8 int D[SIZE]; 9 int N,E; 10 struct Node 11 { 12 int from,to,cost; 13 }Edge[SIZE * 4]; 14 15 void Bellman_Ford(int); 16 void relax(int,int,int); 17 int main(void) 18 { 19 int t; 20 Node temp; 21 22 scanf("%d%d",&t,&N); 23 while(t --) 24 { 25 scanf("%d%d%d",&temp.from,&temp.to,&temp.cost); 26 Edge[E ++] = temp; 27 swap(temp.from,temp.to); 28 Edge[E ++] = temp; 29 } 30 Bellman_Ford(N); 31 printf("%d\n",D[1]); 32 33 return 0; 34 } 35 36 void Bellman_Ford(int s) 37 { 38 fill(D,D + SIZE,INF); 39 D[s] = 0; 40 41 for(int i = 0;i < N - 1;i ++) 42 { 43 UPDATE = false; 44 for(int j = 0;j < E;j ++) 45 relax(Edge[j].from,Edge[j].to,Edge[j].cost); 46 if(!UPDATE) 47 return ; 48 } 49 } 50 51 void relax(int from,int to,int cost) 52 { 53 if(D[to] > D[from] + cost) 54 { 55 D[to] = D[from] + cost; 56 UPDATE = true; 57 } 58 }
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 using namespace std; 5 6 const int SIZE = 1005; 7 const int INF = 0x5fffffff; 8 int N,D[SIZE]; 9 bool IN_QUE[SIZE]; 10 struct Node 11 { 12 int to,cost; 13 }; 14 vector<Node> G[SIZE]; 15 16 void spfa(int); 17 bool relax(int,int,int); 18 int main(void) 19 { 20 int t,from; 21 Node temp; 22 23 scanf("%d%d",&t,&N); 24 while(t --) 25 { 26 scanf("%d%d%d",&from,&temp.to,&temp.cost); 27 G[from].push_back(temp); 28 swap(from,temp.to); 29 G[from].push_back(temp); 30 } 31 spfa(N); 32 printf("%d\n",D[1]); 33 34 return 0; 35 } 36 37 void spfa(int s) 38 { 39 int vec,cost; 40 queue<int> que; 41 fill(D,D + SIZE,INF); 42 D[s] = 0; 43 IN_QUE[s] = true; 44 que.push(s); 45 46 while(!que.empty()) 47 { 48 int cur = que.front(); 49 int len = G[cur].size(); 50 IN_QUE[cur] = false; 51 que.pop(); 52 53 for(int i = 0;i < len;i ++) 54 { 55 vec = G[cur][i].to; 56 cost = G[cur][i].cost; 57 if(relax(cur,vec,cost) && !IN_QUE[vec]) 58 { 59 IN_QUE[vec] = true; 60 que.push(vec); 61 } 62 } 63 } 64 } 65 66 bool relax(int from,int to,int cost) 67 { 68 if(D[to] > D[from] + cost) 69 { 70 D[to] = D[from] + cost; 71 return true; 72 } 73 return false; 74 }