poj 3268 Silver Cow Party

Silver Cow Party
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22580   Accepted: 10333

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

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.

Source

/*
* @Author: Lyucheng
* @Date:   2017-07-21 15:18:48
* @Last Modified by:   Lyucheng
* @Last Modified res: 2017-07-21 15:31:57
*/
/*
 题意:n头牛,然后有m条单向边,聚会的地点在x,每头牛要去x点聚会,并且要来回走最短路,问你这些路程中最长的是多少

 思路:最短路跑一下,然后直接来回判断一下就行了
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>

#define MAXN 1005
#define INF 0x3f3f3f3f

using namespace std;

int n,m,x;  
int u,v,z;
int res[MAXN];//记录每个的时间

struct HeapNode //Dijkstra算法用到的优先队列的节点
{
    int d,u;
    HeapNode(int d,int u):d(d),u(u){}
    bool operator < (const HeapNode &rhs)const
    {
        return d > rhs.d;
    }
};

struct Edge     //
{
    int from,to,dist;
    Edge(int f,int t,int d):from(f),to(t),dist(d){}
};

struct Dijkstra
{
    int n,m;            //点数和边数,编号都从0开始
    vector<Edge> edges; //边列表
    vector<int> G[MAXN];//每个节点出发的边编号(从0开始编号)
    bool done[MAXN];    //是否已永久标号
    int d[MAXN];        //s到各个点的距离
    int p[MAXN];        //p[i]为从起点s到i的最短路中的最后一条边的编号

    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)
    {//如果是无向图,每条无向边调用两次AddEdge
        edges.push_back(Edge(from,to,dist));
        m = edges.size();
        G[from].push_back(m-1);
    }

    void dijkstra(int s)//求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(){ 
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    while(scanf("%d%d%d",&n,&m,&x)!=EOF){
        x--;
        DJ.init(n);
        for(int i=0;i<m;i++){
            scanf("%d%d%d",&u,&v,&z);
            u--;
            v--;
            DJ.AddEdge(u,v,z);
        }
        for(int i=0;i<n;i++){
            res[i]=0;
            if(i==x) continue;
            DJ.dijkstra(i);
            res[i]=DJ.d[x];
        }
        int _max=-1;
        DJ.dijkstra(x);
        for(int i=0;i<n;i++){
            if(i==x) continue;
            res[i]+=DJ.d[i];
            _max=max(_max,res[i]);
        }
        printf("%d\n",_max);
    }
    return 0;
}

 

posted @ 2017-07-21 16:16  勿忘初心0924  阅读(137)  评论(0编辑  收藏  举报