POJ 3268 Silver Cow Party

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


既然是问来回的距离,把矩阵转置一下就可以算啦~

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<cstdio>
using namespace std;
int n,m,x;
const int  MAXINT=0x3f3f3f3f;
const int MAXNUM=1100;
int dist[MAXNUM];
int A[MAXNUM][MAXNUM];
void Dijkstra(int v0)
{
    bool S[MAXNUM];// 判断是否已存入该点到S集合中
    for(int i=1; i<=n; ++i)
    {
        dist[i]=A[v0][i];
       // printf("%d    ",dist[i]);
        S[i]=false; // 初始都未用过该点
    }
   // dist[v0] = 0;
    S[v0]=true;
    for(int i=2; i<=n; i++)
    {
        int mindist = MAXINT;
        int u = -1;// 找出当前未使用的点j的dist[j]最小值
        for(int j=1; j<=n; ++j)
        if((!S[j]) && dist[j]<mindist)
        {
            u = j;// u保存当前邻接点中距离最小的点的号码
            mindist = dist[j];
            //printf("%d        ",mindist);
        }
        S[u] = true;
        for(int j=1; j<=n; j++)
            if((!S[j]) && A[u][j]<MAXINT)
            {
                if(dist[u] + A[u][j] < dist[j])     //在通过新加入的u点路径找到离v0点更短的路径
                {
                    dist[j] = dist[u] + A[u][j];    //更新dis                    //记录前驱顶点
                }
            }
    }
    return;
}
int main()
{
    //freopen("cin.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&x))
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=i+1; j<=n; j++)
                A[i][j]=A[j][i]=MAXINT;
        }
        for(int i=0; i<m; i++)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            A[u][v]=w;
            //printf("%d %d %d\n",u,v,A[u][v]);
        }
        Dijkstra(x);
        int a1[MAXNUM],a2[MAXNUM];
        for(int i=1;i<=n;i++) a1[i]=dist[i];//printf("%d ",a1[i]);
       // puts("");
        for(int i=1;i<=n;i++)
            for(int j=i+1;j<=n;j++)
            {
                int tmp;
                tmp=A[i][j];
                A[i][j]=A[j][i];
                A[j][i]=tmp;
            }
        Dijkstra(x);
        for(int i=1;i<=n;i++) a2[i]=dist[i];
        int minn=0;
        for(int i=1;i<=n;i++)
        {
            minn=max(minn,a1[i]+a2[i]);
        }
        printf("%d\n",minn);
    }
    return 0;
}


你可能感兴趣的:(算法,poj,最短路)