POJ 3268Silver Cow Party

来源:互联网 发布:维珍澳航 知乎 编辑:程序博客网 时间:2024/06/13 05:47

Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18516 Accepted: 8464

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

Line 1: Three space-separated integers, respectively: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.

Output

Line 1: One integer: the maximum of time any one cow must walk.

Sample Input

4 8 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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.

Source

USACO 2007 February Silver




感觉没玩太大的花样,比较裸的最短路了。可是就是不会,感觉好难啊哭

一个是这是单项的路,另一个是要把图正着算(算以x为终点,个点的距离)一次然后倒过来再算(算以x为起点到个点的距离)一次(这么做为了方便最后选出最大)

具体还有点小问题没弄懂……但是我感觉好累脑子跑不动了……翻译也暂时不写了好了OTZ明天解决一下,先把代码贴上吧……



2016.8.5

好的,早上脑子果然清醒。if ( i == j ) map[i][j] = 0 是因为最后算的时候相等的两个点的路我也加进去了!其实可以直接memset(map,INF,sizeof(map))这么赋值,最后结果相加的时候去点i == j的情况就行了

题意:http://jijiwaiwai163.blog.163.com/blog/static/18629621120111018233997/     发现这个博客里详细翻译了,我自己就不写了=。=


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define N 1010#define INF 0x3f3f3f3fint n,m,x;int map[N][N],dis[N],vis[N],disb[N];  int dijkstra(){int i,j,k;memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){dis[i]=map[x][i];disb[i]=map[i][x];}for(i=1;i<=n;i++){int tmp=INF;for(j=1;j<=n;j++)if(!vis[j]&&dis[j]<tmp){tmp=dis[j];k=j;}vis[k]=1;for(j=1;j<=n;j++)if(!vis[j]&&dis[j]>map[k][j]+dis[k])dis[j]=map[k][j]+dis[k];}memset(vis,0,sizeof(vis));for(i=1;i<=n;i++){int tmp=INF;for(j=1;j<=n;j++)if(!vis[j]&&disb[j]<tmp)tmp=disb[j],k=j;vis[k]=1;for(j=1;j<=n;j++)if(!vis[j]&&disb[j]>disb[k]+map[j][k])disb[j]=disb[k]+map[j][k];}int maxx=-1;for(i=1;i<=n;i++)maxx=max(maxx,dis[i]+disb[i]);return maxx;}int main(){int i,j,a,b,w;while(~scanf("%d%d%d",&n,&m,&x)){for(i=1;i<=n;i++){for(j=1;j<=n;j++){if(i==j)map[i][j]=0;else map[i][j]=INF;}}//memset(map,INF,sizeof(map));←这么赋值会wa=。=…… for(i=1;i<=m;i++){scanf("%d%d%d",&a,&b,&w);map[a][b]=w;}int end=dijkstra();printf("%d\n",end);}return 0;}



0 0
原创粉丝点击