poj3268Silver Cow Party最短路问题(dijkstra+邻接矩阵转置)

来源:互联网 发布:淘宝做单一个月挣多少 编辑:程序博客网 时间:2024/06/05 02:49

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 ≤XN). A total ofM (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi requiresTi (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:N,M, andX
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers:Ai,Bi, andTi. The described road runs from farmAi to farmBi, requiringTi 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.
题意是n个农场,每个农场有一头牛去参加派对,派对的地点在x农场,一共有m条路。求所有牛到派对和从派对回自己农场最小花费时间之和的最大值。注意的是该图是有向图,从派对农场回自己农场很好求,用一次dijstra,再把邻接矩阵转置再用一i次dijstra,求出各奶牛从自己从自己农场到派对农场的最小花费时间。

#include<stdio.h>

#include<queue>
#include<algorithm>
#include<memory.h>
#include<iostream>
using namespace std;
#define INF 1<<28
#define MAX 1005
int n,m,x,i,j;
int map[MAX][MAX],dto[MAX],dfrom[MAX];
bool visto[MAX],visfrom[MAX];
typedef pair<int ,int>exe;
void dijstrato()
{
    memset(visto,0,sizeof(visto));
    for(i=1;i<=n;i++)
        dto[i]=INF;
    dto[x]=0;
    priority_queue<exe,vector<exe>,greater<exe> >q;
    q.push(make_pair(dto[x],x));
    while(!q.empty())
    {
        exe tmp=q.top();
        q.pop();
        int now=tmp.second;
        if(visto[now])
            continue;
        visto[now]=true;
        for(i=1;i<=n;i++)
            if(!visto[i]&&map[now][i]<INF&&dto[i]>dto[now]+map[now][i])
        {
            dto[i]=dto[now]+map[now][i];
            q.push(make_pair(dto[i],i));
        }
    }
    return;

}
void dijstrafrom()
{
    memset(visfrom,0,sizeof(visfrom));
    for(i=1;i<=n;i++)
      dfrom[i]=INF;
    dfrom[x]=0;
    priority_queue<exe,vector<exe>,greater<exe> >q;
    q.push(make_pair(dfrom[x],x));
    while(!q.empty())
    {
        exe tmp=q.top();
        q.pop();
        int now=tmp.second;
        if(visfrom[now])
            continue;
        visfrom[now]=true;
        for(i=1;i<=n;i++)
            if(!visfrom[i]&&map[now][i]<INF&&dfrom[i]>map[now][i]+dfrom[now])
            {
                dfrom[i]=dfrom[now]+map[now][i];
                q.push(make_pair(dfrom[i],i));
            }
    }
    return ;

}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&x))
    {
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            map[i][j]=INF;
            int x,y,cost;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&cost);
            map[x][y]=cost;
        }
        dijstrato();
        for(i=1;i<=n;i++)
            for(j=i+1;j<=n;j++)
            swap(map[i][j],map[j][i]);
        dijstrafrom();
        priority_queue<int>p;
        for(i=1;i<=n;i++)
            p.push(dto[i]+dfrom[i]);
        printf("%d\n",p.top());

    }
    return 0;
}

0 0
原创粉丝点击