POJ 3268Silver Cow Party最短路

http://poj.org/problem?id=3268

Language:Default

Silver Cow Party

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30726   Accepted: 13925

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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1000+5;
#define INF 1e8
int n,m,x;
struct Edge
{
    int from,to,dist;
    Edge() {}
    Edge(int f,int t,int d):from(f),to(t),dist(d) {}
};

struct HeapNode
{
    int d,u;
    HeapNode() {}
    HeapNode(int d,int u):d(d),u(u) {}
    bool operator < (const HeapNode &rhs)const
    {
        return d > rhs.d;
    }
};

struct Dijkstra
{
    int n,m;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool done[maxn];
    int d[maxn];
    int p[maxn];

    void init(int n)
    {
        this->n=n;
        for(int i=0; i<n; i++) G[i].clear();
        edges.clear();
    }

    void AddEdge(int from,int to,int dist)
    {
        edges.push_back(Edge(from,to,dist) );
        m = edges.size();
        G[from].push_back(m-1);
    }

    void dijkstra(int s)
    {
        priority_queue<HeapNode> Q;
        for(int i=0; i<n; i++) d[i]=INF;
        d[s]=0;
        memset(done,0,sizeof(done));
        Q.push(HeapNode(0,s) );

        while(!Q.empty())
        {
            HeapNode x=Q.top();
            Q.pop();
            int u=x.u;
            if(done[u]) continue;
            done[u]= true;

            for(int i=0; i<G[u].size(); i++)
            {
                Edge& e= edges[G[u][i]];
                if(d[e.to]> d[u]+e.dist)
                {
                    d[e.to] = d[u]+e.dist;
                    p[e.to] = G[u][i];
                    Q.push(HeapNode(d[e.to],e.to) );
                }
            }
        }
    }
} DJ;

int main()
{
    while(scanf("%d%d%d",&n,&m,&x)==3)
    {
        DJ.init(n);
        for(int k=1; k<=m; k++)
        {
            int i,j,d;
            cin>>i>>j>>d;
            DJ.AddEdge(i-1,j-1,d);
        }
        int maxx=-1;
        for(int i=0; i<n; i++)
        {
            DJ.dijkstra(i);
            int x1=DJ.d[x-1];
           // printf("%d\n",x1);
            DJ.dijkstra(x-1);
            int x2=DJ.d[i];
            maxx=max(maxx,x1+x2);
        }
        printf("%d\n",maxx);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/85108308