1. 程式人生 > >poj3268 Dijkstra演算法 處理往返路問題

poj3268 Dijkstra演算法 處理往返路問題

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

有N頭牛要去參加牛X那裡的聚會,現在除了X牛外,其他N-1頭牛都要走到X牛那裡去.給你M條有向邊,現在問你任意一頭牛從自己的位置走到X牛那,然後再走回來(來回都選擇最短路徑走)的話,需要的總時間的最大值是多少?即從所有N-1頭牛中找那個最大的來回時間.

思路:

  首先本題的有向圖,所以從X點到其他所有點的最短距離就是所有牛回家需要走的路.

然後把所有的邊方向反過來,那麼x點到其他點的最短距離就是所有牛來時所要走的路。

然後找打一頭牛往返的最長時間就是答案。

程式碼:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=10000;
const int inf=0x3ffffff;
struct Edge
{
    int from,to,dist;
    Edge(){}
    Edge(int f,int t,int d):from(f),to(t),dist(d){}

};
struct Head
{
    int d;int u;
   // Head(){}
    Head(int dd,int uu):d(dd),u(uu){}
   bool operator<(const Head& ff)  const
    {
        return d>ff.d;
    }
};
struct dislal
{
    int n,m;
    vector<Edge> edges;
    vector<int> g[maxn];
    bool done[maxn];
    int d[maxn];
    void intn(int n)
    {
        this->n=n;
        for(int i=0;i<n;i++) g[i].clear();
        edges.clear();
    }
    void addedge(int from,int t,int d)
    {
        edges.push_back(Edge(from,t,d));
        int m=edges.size();
        g[from].push_back(m-1);
    }
    void dij(int s)
    {
         priority_queue<Head> Q;
        for(int i=0;i<n;i++) d[i]=inf;
        d[s]=0;
        memset(done,0,sizeof(done));
        Q.push(Head(0,s));
        while(!Q.empty())
        {
            //Head p=Q.;  Q.pop();

            Head p=Q.top(); Q.pop();
            int x=p.u;
            if(done[x])continue;
            done[x]=1;
            for(int i=0;i<g[x].size();i++)
            {
                Edge &e=edges[g[x][i]];
                if(d[e.to]>d[x]+e.dist)
                {
                    d[e.to]=d[x]+e.dist;
                    Q.push(Head(d[e.to],e.to));
                }
            }
        }

    }
}dj1,dj2;
int main()
{
    int n,m,s;
    cin>>n>>m>>s;
    s--;
    dj1.intn(n);
     dj2.intn(n);
     int x,y,w;
     for(int i=1;i<=m;i++)
     {
         cin>>x>>y>>w;
         x--;
         y--;
         dj1.addedge(x,y,w);
         dj2.addedge(y,x,w);
     }
     dj1.dij(s);
     dj2.dij(s);
     int maxt=-1;
     for(int i=0;i<n;i++)
     {
         maxt=max(maxt,(dj1.d[i]+dj2.d[i]));
     }
     cout<<maxt;
}