POJ Silver Cow Party

来源:互联网 发布:员工工作证制作软件 编辑:程序博客网 时间:2024/05/21 11:06

https://cn.vjudge.net/problem/11757/origin

  

Language:
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 22050 Accepted: 10070

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


题意:有N个庄园,每个庄园都有一头奶牛,有M条路,在X处庄园举办Party,每头牛都需要到X处庄园,且还要回去,一去一回中每头奶牛用时都是最短,求其中最长的距离。(注意:路是单向的。)

思路:第一次用dijkstra算法,从X庄园开始。第二次,把路径变返,在用一次dijkstra算法。最后相加求最大值。
例如:1 2 3
             2 3 1
             3 4 5
开始时 map[1][2]=3;
             map[2][3]=1;
             map[3][4]=5;
第一次用dijkstra算法,求的是回去所用的时间。
             map[2][1]=3;
             map[3][2]=1;
             map[4][3]=5;
第二次用dijkstra算法,求的是去的所用的时间。
#include<stdio.h>#include<string.h>#define INF 0x3f3f3f3f#define MAX1 1000+10int book[MAX1],map[MAX1][MAX1],vis1[MAX1];int dis[101000][3],n,m,a,vis[MAX1];void F(){    for(int i=0; i<m; i++)    {        int x=dis[i][0];        int y=dis[i][1];        int z=dis[i][2];        if(z<map[y][x])            map[y][x]=z;    }    return ;}void djikrsta(){    int i,j,MAX2,u;    memset(book,0,sizeof(book));    memset(vis,INF,sizeof(vis));    for(i=1; i<=n; i++)        vis[i]=map[a][i];    book[a]=1;    for(i=1; i<=n; i++)    {        MAX2=INF ,u=i;        for(j=1; j<=n; j++)            if(!book[j]&&vis[j]<MAX2)            {                MAX2=vis[j];                u=j;            }        book[u]=1;        for(j=1; j<=n; j++)            if(!book[j]&&vis[j]>map[u][j]+vis[u])                vis[j]=map[u][j]+vis[u];    }}void huan(){    for(int i=1; i<=n; i++)        vis1[i]=vis[i];}void chushihua(){    for(int i=1; i<=n; i++)        for(int j=1; j<=n; j++)            if(i==j)                map[i][j]=0;            else map[i][j]=INF;}int main(){    while(~scanf("%d %d %d",&n,&m,&a))    {        int i,j,max3=0,x,y,z;        chushihua();        for(i=0; i<m; i++)        {            scanf("%d %d %d",&dis[i][0],&dis[i][1],&dis[i][2]);            x=dis[i][0];            y=dis[i][1];            z=dis[i][2];            if(z<map[x][y])                map[x][y]=z;        }        djikrsta();        huan();        chushihua();        F();        djikrsta();        for(i=1; i<=n; i++)            if(vis[i]+vis1[i]>max3)                max3=vis[i]+vis1[i];        printf("%d\n",max3);    }    return 0;}







原创粉丝点击