您的位置:首页 > 其它

POJ 3268 Silver Cow Party(最短路dijkstra)

2015-08-15 08:40 363 查看
Silver Cow Party

Time Limit: 2000MSMemory Limit: 65536K
Total Submissions: 15451Accepted: 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 ≤
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:
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 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.

解题思路:题意抽象出来意思是给一个有向图,然后求所有点经过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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: