1. 程式人生 > >POJ 3268 迪傑斯特拉圖論 置換找最短路

POJ 3268 迪傑斯特拉圖論 置換找最短路

繼續 with style pro end #define farm sca .net

題目:https://vjudge.net/problem/POJ-3268

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. 題意:一群牛分別從1~n號農場趕往x號農場參加聚會,農場與農場之間的路時單向的,在n個農場之間有m條路, 給出 a ,b , t表示從a號農場到b號農場需要t時間。 每頭牛都會選擇最短的路,問來回路上(i→x+x→i)花費時間最長的牛花費的時間是多少? 思路: 正向建立圖查x到各點最短路,,,,在置換mp[j][i]=mp[i][j];;再查x到各點的最短路 然後兩次求得最短路相加查找最大值 ( 復雜度 O(n*n)) 代碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<algorithm>
#include<map>
#define maxn 200005
#define inf 100000000
using namespace std;
int dis[maxn];
int mp[1005][1005];
int n,m,x;
int v,u,cost;
bool vis[maxn];
int cnt[maxn];
void dijkstra(int n,int v)
{
    bool vis[maxn];
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dis[i]=mp[v][i];
    }
    dis[v]=0;
    vis[v]=true;
    for(int i=2;i<=n;i++)
    {
            int u=v;
            int mazz=inf;
            for(int j=1;j<=n;j++){
        if(!vis[j]&&dis[j]<mazz)//換dis最小的頂點繼續查找
        {
                u=j;
                mazz=dis[j];
        }
        }
        vis[u]=true;
        for(int k=1;k<=n;k++)//更新頂點上的dis
        {
            if(!vis[k]&&mp[u][k]<inf)
            {
                if(dis[k]>mp[u][k]+dis[u]){
                    dis[k]=mp[u][k]+dis[u];
                }
            }
        }

    }
}
int main()
{
   scanf("%d%d%d",&n,&m,&x);
   memset(mp,inf,sizeof(mp));
    //memset(mp,inf,sizeof(mp));
    for(int i=1;i<=n;i++)
    for(int j=1;j<=n;j++)
    mp[i][j]=inf;
    for(int i=0;i<m;i++){
        cin>>u>>v>>cost;
        mp[u][v]=cost;
    }
    for(int i=1;i<=n;i++)
    {
        dis[i]=inf;
        cnt[i]=inf;
    }
    dijkstra(n,x);
    for(int i=1;i<=n;i++)
    {
        cnt[i]=dis[i];
    }
    for(int i=1;i<=n;i++)
    {
        dis[i]=inf;
    }
   for(int i=1;i<=n;++i)
        {
            for(int j=i+1;j<=n;++j)
            {
                int aa;
                aa=mp[j][i];
                mp[j][i]=mp[i][j];
                mp[i][j]=aa;
            }
        }
        dijkstra(n,x);
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            if(i!=x);
            ans=max(ans,dis[i]+cnt[i]);
        }
        printf("%d\n",ans);
}

簡單的圖論思路題 ,,,不過題目挺好的

POJ 3268 迪傑斯特拉圖論 置換找最短路