1. 程式人生 > >POJ-3268-Silver Cow Party(迪傑斯特拉 多點到star和star到多點)

POJ-3268-Silver Cow Party(迪傑斯特拉 多點到star和star到多點)

D - Silver Cow Party
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

POJ 3268
Appoint description:
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.

題意:首行給出N,M,X代表有N個點,M條邊,接著M行每行u,v,w代表u到v權值為w,這是個有向圖,求出各點到X加上X到各點最小權值中的最大值。

思路:迪傑斯特拉的變種,dis[i]=map[star][i]改成dis[i]=map[i][star]就是第二種情況
判斷時改為dis[j]>dis[point]+map[j][point]?dis[j]=dis[point]+map[j][point]即可

程式碼

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<iomanip>
using namespace std;
//有向圖各點到star和star到各點最短路
const int maxn=1005;
const int INF=0x3f3f3f3f;
int map[maxn][maxn];//有向圖
int dis_to[maxn];//star到各點最短距離
int dis_from[maxn];//各點到star最短距離
bool vis_to[maxn];
bool vis_from[maxn];
int N;//圖大小
void Dijkstra(int star)
{
    for(int i=1; i<=N; i++)
    {
        dis_to[i]=map[star][i];//star到各點
        dis_from[i]=map[i][star];//各點到star
        vis_from[i]=vis_to[i]=0;
    }
    vis_from[star]=vis_to[star]=1;
    for(int i=1; i<N; i++) //star到各點最短路
    {
        int minn=INF;
        int point;
        for(int j=1; j<=N; j++)
            if(vis_to[j]==0&&dis_to[j]<minn)
            {
                minn=dis_to[j];
                point=j;
            }
        if(minn==INF)
            break;
        vis_to[point]=1;
        for(int j=1; j<=N; j++)
            if(vis_to[j]==0&&dis_to[j]>dis_to[point]+map[point][j])
                dis_to[j]=dis_to[point]+map[point][j];
    }
    for(int i=1; i<N; i++) //各點到star最短路
    {
        int minn=INF;
        int point;
        for(int j=1; j<=N; j++)
            if(vis_from[j]==0&&dis_from[j]<minn)
            {
                minn=dis_from[j];
                point=j;
            }
        if(minn==INF)
            break;
        vis_from[point]=1;
        for(int j=1; j<=N; j++)
            if(vis_from[j]==0&&dis_from[j]>dis_from[point]+map[j][point])
                dis_from[j]=dis_from[point]+map[j][point];
    }
}
int main()
{
    int M,X;
    scanf("%d%d%d",&N,&M,&X);
    for(int i=1; i<=N; i++)
        for(int j=1; 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(map[u][v]>w)
            map[u][v]=w;//有向圖
    }
    Dijkstra(X);
    int max_num=-1;
    for(int i=1; i<=N; i++)
        if(dis_to[i]+dis_from[i]>max_num)
            max_num=dis_to[i]+dis_from[i];
    printf("%d\n",max_num);
    return 0;
}