poj3268(单源最短路,dijkstra算法)

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.

有很多编好号的奶牛,有各种有向线段表示从一个奶牛到另一个奶牛需要的时间,然后求出所有奶牛来某个具体编号x奶牛再回去的最短时间的最大值,首先就是以x为起点到各个奶牛的最短路都是可以求出来的吧,然后怎么办,这只是算出来了各个奶牛回去的最短时间啊!那来的怎么算?不可能一个个遍历吧。。。。。。所以有大牛就想出来方法将所有方向反向之后再求一遍以x为起点的最短时间,就表示所有奶牛来的最短时间~~~~所以,只是在最短路的代码基础上又加上了另一个反向的最短路而已~~~

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAX=100000000;
int a[1010][1010],aa[1010][1010],dist[100010],dist2[100010],s[100010],s2[100010];
int v,n,m,x;
void ShortestPath()
{
    int i, j, u, temp, newdist,u2,temp2,newdist2;
    if(v<1 || v>n)    return;
    for (i=1; i<= n; i++)
    {
        dist[i]=a[v][i];
        dist2[i]=aa[v][i];
        s[i] =s2[i]= 0;
    }
    dist[v] = dist2[v]=0, s[v] = s2[v]=1;
    for (i = 2; i <= n; i++)
    {
        temp =temp2= MAX;
        u = u2=v;
        for (j = 1; j <= n; j++)
            {
                if ((!s[j])&&(dist[j]<temp))
            {
                u = j;
                temp = dist[j];
            }
            if ((!s2[j])&&(dist2[j]<temp2))
            {
                u2 = j;
                temp2 = dist2[j];
            }
            }
        s[u] = s2[u2]=1;
        for (int j = 1; j <= n; j++)
            {
                if((!s[j])&&(a[u][j]<MAX))
            {
                newdist = dist[u]+a[u][j];
                if (newdist<dist[j])    dist[j] = newdist;
            }
            if((!s2[j])&&(aa[u2][j]<MAX))
            {
                newdist2 = dist2[u2]+aa[u2][j];
                if (newdist2<dist2[j])    dist2[j] = newdist2;
            }
            }
    }
    int ans=0;
    for(int i=1;i<=n;i++)
    if(i!=x)
    ans=max(dist[i]+dist2[i],ans);
    printf("%d\n",ans);
}

int main()
{

        for(int i=0;i<1010;i++)
        for(int j=0;j<1010;j++)
        a[i][j]=aa[i][j]=MAX;
        scanf("%d%d%d",&n,&m,&x);
        for(int i=0;i<m;i++)
        {
            int a1,a2,ww;
            scanf("%d%d%d",&a1,&a2,&ww);
            a[a1][a2]=ww;
            aa[a2][a1]=ww;
        }
        v=x;
        ShortestPath();

    return 0;
}

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/martinue/article/details/47788525
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞