Til the Cows Come Home 最短路径
2016-11-24 15:48
351 查看
Til the Cows Come Home
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status
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
Sample Output
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
这个题一开始提交的时候一直RE,我就想到底哪里栈溢出了,后来发现是双向边,也就是存储边的数组至少要开到4000,这个题有可能会出现重边,也就是两个点之间可能会有多条边
AC代码:
方法一: Bellman-ford算法,该算法可以解决重边问题
Dijkstra算法,构建dis数组是要解决重边数组
AC代码:
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status
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.
这个题一开始提交的时候一直RE,我就想到底哪里栈溢出了,后来发现是双向边,也就是存储边的数组至少要开到4000,这个题有可能会出现重边,也就是两个点之间可能会有多条边
AC代码:
方法一: Bellman-ford算法,该算法可以解决重边问题
//#include<bits/stdc++.h> #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<queue> #include<algorithm> #include<map> #include<iomanip> #define inf 0x3f3f3f3f #define maxn 1002 #define maxm 4004 //因为是是双向边 using namespace std; int n,m; int head[maxn],dis[maxn]; int book[maxn]; struct node { int x,y,w; int next; }; node edge[maxm]; int main() { queue<int> q; scanf("%d%d",&m,&n); int i; for(i = 0;i < m;++i) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].w); for(i = m;i < 2*m;++i) { edge[i].x = edge[i-m].y; edge[i].y = edge[i-m].x; edge[i].w = edge[i-m].w; } memset(head,-1,sizeof(head)); for(i = 0;i < 2*m;++i) { edge[i].next = head[edge[i].x]; head[edge[i].x] = i; } dis = 0; for(i = 1;i < n;++i) dis[i] = inf; book = 1; q.push(n); while(!q.empty()) { int k = head[q.front()]; while(k != -1) { if(dis[edge[k].y] > dis[q.front()] + edge[k].w) { dis[edge[k].y] = dis[q.front()] + edge[k].w; if(!book[edge[k].y]) { book[edge[k].y] = 1; q.push(edge[k].y); } } k = edge[k].next; } book[q.front()] = 0; q.pop(); } printf("%d\n",dis[1]); return 0; }方法二:
Dijkstra算法,构建dis数组是要解决重边数组
AC代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 1002; const int maxm = 4004; const int inf = 0x3f3f3f3f; struct node { int x, y, w; int next; }; node edge[maxm]; int dis[maxn], book[maxn], head[maxn]; int main() { int t, n; scanf("%d%d",&t, &n); int i; for(i=0; i<t; ++i) scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].w); for(i=t; i<2*t; ++i) { edge[i].x = edge[i-t].y; edge[i].y = edge[i-t].x; edge[i].w = edge[i-t].w; } memset(head 4000 , -1, sizeof(head)); for(i=0; i<2*t; ++i) { edge[i].next = head[edge[i].x]; head[edge[i].x] = i; } memset(dis, inf, sizeof(dis)); int k = head ; dis = 0; while(k != -1) { if(dis[edge[k].y] < edge[k].w) //解决重边问题 dis[edge[k].y] = edge[k].w; k = edge[k].next; } book = 1; int cnt = 0; while(cnt < n-1) { int Min = inf, t = n; for(i=1; i<n; ++i) { if(!book[i] && dis[i] < Min) { Min = dis[i]; t = i; } } book[t] = 1; ++cnt; k = head[t]; while(k != -1) { if(!book[edge[k].y] && dis[edge[k].y] > dis[t] + edge[k].w) dis[edge[k].y] = dis[t] +edge[k].w; k = edge[k].next; } } printf("%d\n", dis[1]); return 0; }
相关文章推荐
- POJ2387 Til the Cows Come Home(最短路径)
- 【A - Til the Cows Come Home 】最短路径
- POJ 2387 Til the Cows Come Home -最短路径
- POJ 2387 Til the Cows Come Home --单源最短路径
- poj 2387 Til the Cows Come Home(最短路径)
- Til the Cows Come Home(最短路径)
- Til the Cows Come Home (最短路径)
- 百练OJ-( Til the Cows Come Home)dijksra最短路径
- Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))
- POJ_2387_Til the Cows Come Home(USACO 2004 November)_最短路径
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
- poj2387——Til the Cows Come Home(最短路径)
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
- (阶段三 dijkstra算法温习 1.6)POJ 2387 Til the Cows Come Home(使用dijkstra算法求单源起点和单源终点的最短路径)
- POJ 2387 Til the Cows Come Home (最短路径,Dijkstra算法)
- POJ 2387 Til the Cows Come Home (图论,最短路径)
- A - Til the Cows Come Home(最短路径)
- POJ 2387 Til the Cows Come Home(最短路径)
- POJ-2387 Til the Cows Come Home(Dijsktra算法求最短路径)
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径