本文主要是介绍【POJ】 3268 Silver Cow Party,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 16290 | Accepted: 7460 |
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.
Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way.
Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?
Input
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Sample Input
4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
题解:这一题就是做两次单元最短路径,不同的就是把所有边反向就好。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>using namespace std;struct Edge
{int from,to,d,next;
};
struct Edge e[100005];
int n,l,m,x,ans[1005],d[1005],vis[1005],pos[1005];
queue<int> q;int spfa(int s)
{for (int i=1;i<=n;i++) d[i]=10000000;d[s]=0;int x;q.push(s); vis[s]=1;while (!q.empty()){x=q.front(); q.pop(); vis[x]=0;for (int i=pos[x];i!=0;i=e[i].next)if (d[e[i].to]>d[e[i].from]+e[i].d){d[e[i].to]=d[e[i].from]+e[i].d;if (!vis[e[i].to]){vis[e[i].to]=1;q.push(e[i].to);}}}return 1;
}void reset()
{memset(pos,0,sizeof(pos));for (int i=1;i<=m;i++){int x;x=e[i].from; e[i].from=e[i].to; e[i].to=x;e[i].next=pos[e[i].from]; pos[e[i].from]=i;}
}int main()
{scanf("%d%d%d",&n,&m,&x);for (int i=1;i<=m;i++){scanf("%d%d%d",&e[i].from,&e[i].to,&e[i].d);e[i].next=pos[e[i].from]; pos[e[i].from]=i;}spfa(x);for (int i=1;i<=n;i++) ans[i]+=d[i];reset();spfa(x);for (int i=1;i<=n;i++) ans[i]+=d[i];for (int i=1;i<=n;i++) l=max(ans[i],l);printf("%d",l);return 0;
}
这篇关于【POJ】 3268 Silver Cow Party的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!