Til the Cows Come Home
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 37662 | Accepted: 12836 |
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
3 4 204 5 202 3 30 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.
SPFA:
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 8 using namespace std; 9 const int INF = 10000000;10 const int MAX = 1000 + 10;11 int t,n;12 struct point13 {14 int e,w;15 };16 vector g[MAX];17 int dist[MAX];18 void spfa(int v)19 {20 for(int i = 0; i <= n; i++)21 {22 dist[i] = INF;23 }24 dist[v] = 0;25 queue que;26 que.push(v);27 while(que.size())28 {29 int x = que.front();30 que.pop();31 int len = g[x].size();32 for(int i = 0; i < len; i++)33 {34 int y = g[x][i].e;35 if(dist[y] > dist[x] + g[x][i].w)36 {37 dist[y] = dist[x] + g[x][i].w;38 que.push(y);39 }40 }41 }42 }43 int main()44 {45 while(scanf("%d%d", &t, &n) != EOF)46 {47 for(int i = 0; i < MAX; i++)48 g[i].clear();49 50 while(t--)51 {52 int s,e,w;53 point temp;54 scanf("%d%d%d", &s,&e,&w);55 temp.w = w;56 temp.e = e;57 g[s].push_back(temp);58 temp.e = s;59 g[e].push_back(temp);60 }61 62 spfa(n);63 printf("%d\n",dist[1]);64 }65 66 return 0;67 }
Dijkstra
注意重边问题
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 const int INF = 10000000; 7 const int MAX = 1000 + 10; 8 int g[MAX][MAX],dist[MAX],vis[MAX]; 9 int t,n;10 void Dijkstra()11 {12 for(int i = 1; i <= n; i++)13 dist[i] = INF;14 memset(vis,0,sizeof(vis));15 dist[n] = 0;16 vis[n] = 0;17 int pos = n;18 for(int i = 1; i < n; i++)19 {20 int minn = INF;21 for(int j = 1; j <= n; j++)22 {23 if(vis[j] == 0 && dist[j] < minn)24 {25 minn = dist[j];26 pos = j;27 }28 }29 vis[pos] = 1;30 for(int j = 1; j <= n; j ++)31 {32 if(vis[j] == 0 && dist[j] > dist[pos] + g[pos][j])33 dist[j] = dist[pos] + g[pos][j];34 }35 }36 }37 int main()38 {39 while(scanf("%d%d",&t,&n) != EOF)40 {41 int s,e,w;42 for(int i = 1; i <= n; i++)43 {44 for(int j = 1; j <= n; j++)45 {46 g[i][j] = INF;47 }48 }49 for(int i = 0; i < t; i++)50 {51 scanf("%d%d%d",&s,&e,&w);52 if(g[s][e] > w)53 g[s][e] = g[e][s] = w;54 }55 Dijkstra();56 printf("%d\n",dist[1]);57 }58 59 }
Ballem_ford
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 const int INF = 1000000000; 9 const int MAX = 1000 + 10;10 int n,t;11 struct point12 {13 int s,t,w;14 };15 vector g;16 int dist[MAX];17 void Ballem_ford(int v)18 {19 for(int i = 1; i <= n; i++)20 dist[i] = INF;21 dist[v] = 0;22 for(int j = 1; j < n; j++)23 {24 int len = g.size();25 int flag = 0;26 for(int i = 0; i < len; i++)27 {28 int s = g[i].s;29 int t = g[i].t;30 int w = g[i].w;31 if(dist[t] > dist[s] + w)32 {33 dist[t] = dist[s] + w;34 flag = 1;35 }36 }37 if(flag == 0) //加个flag 优化一下 38 break;39 }40 }41 int main()42 {43 while(scanf("%d%d", &t, &n) != EOF)44 {45 g.clear();46 int s,e,w;47 point temp;48 for(int i = 0; i < t; i++)49 {50 scanf("%d%d%d", &s,&e,&w);51 temp.w = w;52 temp.t = e;53 temp.s = s;54 g.push_back(temp);55 temp.t = s;56 temp.s = e;57 g.push_back(temp);58 }59 Ballem_ford(n);60 printf("%d\n",dist[1]);61 }62 }