POJ 3268 Silver Cow Party(最短路dijkstra)

本文探讨了如何使用两个迪杰斯特拉算法分别求解有向图中从任意点到指定节点和从该节点到任意点的最短路径,并最终找到所有路径中最长的时间消耗。
                                                                                                                                        Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K
Total 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; 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值