poj 3268 Silver Cow Party

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 20792   Accepted: 9515

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 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

123


题意的意思是   有个目的地 然后其余的牛 都到这个目的地 然后在回到 原来的地方 问时间最短多少?

 单向图 去和回来 是不一样的  


不管是去 还是回来 我们都可以化成 从目的地 开始出发 到其余个点 这来来 看最短 路径问题

一次输入数据 可以将邻接矩阵置换  也可以  不置换 两个变量 

套用Dijstra 模板 

/* 
poj  3268  
*/  
#include <iostream>  
#include <stdio.h>
using namespace std;  
#define MAX 1010  
#define inf 1<<29  
  
int map[MAX][MAX],d[MAX],dback[MAX];  
bool vis1[MAX],vis2[MAX];  
int n,m,x;  
  
int Dijkstra(){  
      
    int i,j,u1,u2;  //用到 两个 变量 变量都用两次
    for(i=1;i<=n;i++){  
        vis1[i]=0;
		vis2[i]=0;  
        d[i]=map[x][i];  //这里不同
        dback[i]=map[i][x];  // 相当于矩阵置换
    }  
      
    for(i=1;i<=n;i++){  
        int mid1=inf;
		int mid2=inf;  
        for(j=1;j<=n;j++)  
        {
        	if(!vis1[j] && d[j]<mid1){  
                u1=j;  
                mid1=d[j];  
            }  
            if(!vis2[j] && dback[j]<mid2){  
                u2=j;  
                mid2=dback[j];  
            }  
              
		}
            vis2[u2]=1;
            vis1[u1]=1;  
            for(j=1;j<=n;j++)
			{  
                if(!vis1[j] && map[u1][j]+d[u1]<d[j])  
                    d[j]=map[u1][j]+d[u1];  
                if(!vis2[j] && map[j][u2]+dback[u2]<dback[j])  
                    dback[j]=map[j][u2]+dback[u2];  
            }  
    }  
      
    int ans=0;  
    for(i=1;i<=n;i++)
	{  
        if(d[i]+dback[i]>ans)  
            ans=d[i]+dback[i];  
    }  
    return ans;  
}  
  
int main(){  
    int i,a,b,time,j;  
    while(~scanf("%d%d%d",&n,&m,&x))
	{  
        for(i=1;i<=n;i++){  
            for(j=1;j<=n;j++)  
				 map[i][j]=inf;  
        }  
          
        for(i=1;i<=m;i++)
		{  
            scanf("%d %d %d",&a,&b,&time);  
            map[a][b]=time;  //有向图 
        }  
          
        printf("%d\n",Dijkstra());  
    }  
    return 0;  
}   


posted @ 2017-03-09 17:36  Sizaif  阅读(192)  评论(0编辑  收藏  举报