POJ 3268 Silver Cow Party(最短路dijkstra)

来源:互联网 发布:精通python网络爬虫pdf 编辑:程序博客网 时间:2024/05/29 15:12
                                                                                                                                        Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 15451 Accepted: 6997

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 of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi 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:N, M, and X
Lines 2..M+1: Line i+1 describes road i with three space-separated integers:Ai, Bi, and Ti. The described road runs from farmAi 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 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.

解题思路:题意抽象出来意思是给一个有向图,然后求所有点经过v点,再回到原点的所有最短路径中最长的一条。解题方法就是用两个dijkstra,分别求i到v的最短距离和v到i的最短距离,然后找出最大的即可。

 
<code class="hljs handlebars has-numbering"><span class="xml"><span class="hljs-tag">求最短路径步骤算法步骤如下:<span class="hljs-attribute">G</span>=<span class="hljs-value">{V,E}</span><span class="hljs-attribute">1.</span> 初始时令 <span class="hljs-attribute">S</span>=<span class="hljs-value">{V0},T=V-S={其余顶点},T中顶点对应的距离值</span>若存在<<span class="hljs-attribute">V0</span>,<span class="hljs-attribute">Vi</span>></span>,d(V0,Vi)为<span class="hljs-tag"><<span class="hljs-title">V0,Vi</span>></span>弧上的权值若不存在<span class="hljs-tag"><<span class="hljs-title">V0,Vi</span>></span>,d(V0,Vi)为∞2. 从T中选取一个与S中顶点有关联边且权值最小的顶点W,加入到S中3. 对其余T中顶点的距离值进行修改:若加进W作中间顶点,从V0到Vi的距离值缩短,则修改此距离值重复上述步骤2、3,直到S中包含所有顶点,即W=Vi为止</span></code>


#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespace std;const int maxn=1010;const int INF=0x7ffffff;int load[maxn][maxn];int v[maxn];int d1[maxn],d2[maxn];int n,m,x;int main(){    while(scanf("%d %d %d",&n,&m,&x) != EOF)    {        int a,b,c;        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)              if(i!=j) load[i][j]=INF;              else if (i==j) load[i][i]=0;        for(int i=0;i<m;i++){            scanf("%d %d %d",&a,&b,&c);            load[a][b]=min(load[a][b],c);        }        memset(v,0,sizeof(v));        for(int i=1;i<=n;i++) d1[i]=INF;        d1[x]=0;        for(int i=0;i<n;i++)        {            int x,ans=INF;            for(int y=1;y<=n;y++) if(!v[y] && d1[y]<=ans) {ans=d1[y]; x=y;}            v[x]=1;            for(int y=1;y<=n;y++)                if(d1[y]>d1[x]+load[x][y]) d1[y]=d1[x]+load[x][y];        }        memset(v,0,sizeof(v));        for(int i=1;i<=n;i++) d2[i]=INF;        d2[x]=0;        for(int i=0;i<n;i++)        {            int x,ans=INF;            for(int y=1;y<=n;y++) if(!v[y] && d2[y]<=ans) {ans=d2[y];x=y;}            v[x]=1;            for(int y=1;y<=n;y++)                if(d2[y]>d2[x]+load[y][x]) d2[y]=d2[x]+load[y][x];        }        int ans=0;        for(int i=1;i<=n;i++)        {            if(ans<d1[i]+d2[i]) ans=d1[i]+d2[i];        }        cout<<ans << endl;    }    return 0;}


0 0
原创粉丝点击