poj 3268 Silver Cow Party(迪杰斯特拉)

   Silver Cow Party

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20035 Accepted: 9139
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看成终点。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
using namespace std;
const int INF=0x3f3f3f3f;
#define MAX 1005
int map[MAX][MAX];
int dis1[MAX],dis2[MAX];
bool vis1[MAX],vis2[MAX];
int N,M,X;
void Dijkstra1()//把X看成终点
{
    memset(vis1,0,sizeof(vis1));
    vis1[X]=1;
    for(int i=1; i<=N; i++)
        dis1[i]=map[i][X];//这时dis1内存的便是i到x的距离
    for(int i=1; i<N; i++)
    {
        int minn=INF;
        int point;
        for(int j=1; j<=N; j++)
            if(vis1[j]==0&&dis1[j]<minn)
            {
                minn=dis1[j];
                point=j;
            }
        vis1[point]=1;
        for(int j=1; j<=N; j++)
        {
            if(map[j][point]<INF&&map[j][point]+dis1[point]<dis1[j])//这里需要注意,跟以前不一样
                            dis1[j]=map[j][point]+dis1[point];
        }
    }
}
void Dijkstra2()//X做起点时
{
    memset(vis2,0,sizeof(vis2));
    vis2[X]=1;
    for(int i=1; i<=N; i++)
        dis2[i]=map[X][i];
    for(int i=1; i<N; i++)
    {
        int minn=INF;
        int point;
        for(int j=1; j<=N; j++)
            if(vis2[j]==0&&dis2[j]<minn)
            {
                minn=dis2[j];
                point=j;
            }
        vis2[point]=1;
        for(int j=1; j<=N; j++)
        {
            if(map[point][j]<INF&&map[point][j]+dis2[point]<dis2[j])//这里复制张贴上面的,出bug了,害我找了会bug,唉,复制有毒啊
                dis2[j]=dis2[point]+map[point][j];
        }
    }
}
int main()
{
    scanf("%d%d%d",&N,&M,&X);
    for(int i=0; i<=N; i++)
        for(int j=0; j<=N; j++)
            i==j?map[i][j]=0:map[i][j]=INF;
    while(M--)
    {
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        if(w<map[u][v])
            map[u][v]=w;
    }
    Dijkstra1();
    Dijkstra2();
    int MAXn=-1;
    for(int i=1; i<=N; i++)
    {
        if(dis1[i]+dis2[i]>MAXn)
            MAXn=dis1[i]+dis2[i];
    }
    printf("%d
",MAXn);
    return 0;
}
"No regrets."
原文地址:https://www.cnblogs.com/zxy160/p/7215165.html