【POJ3268】Silver Cow Party (dij/matrix transpose)

Silver Cow Party

Time Limit: 2000msMemory Limit: 65536KB 64-bit integer IO format: %lld Java class name: Main

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

The main idea of ​​the topic: The old cow has a party, and each cow lives in a cowshed (1 to n). Ask you to find the longest journey to and from the old cow among all the cows (the cow thief JB is lazy but very clever, it is the shortest path whether to go or come). Enter three numbers to represent the number of points, the number of sides, and the position of the old cow. This is a directed graph map[a][b]不一定等于map[b][a].

Analysis: In the beginning, according to DIJ's idea, we will start from the old cow's position X and find the shortest route to other points. Then transpose the adjacency matrix (transpose map[][]) and start DIJ again from X, and find the shortest path from other points to X. Add the two arrays that record the shortest path in order to get the largest one and you are done.

#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"

using namespace std;

const int maxn = 1e3+5;
const int INF = 0x3f3f3f3f;

int map[maxn][maxn];
int dis[maxn];
int vis[maxn];
int dd[maxn];
int n,m,x;
int a,b,time;

void dij(){
    memset(vis,0,sizeof(vis));
    memset(dis,INF,sizeof(dis));
    dis[x]=0;
    for( int i=1 ; i<=n ; i++ ){
        int u = -1;
        for( int j=1 ; j<=n ; j++ ){
            if(!vis[j]&&(u==-1||dis[j]<dis[u])){
                u=j;
            }
        }
        vis[u]=1;
        for( int j=1 ; j<=n ; j++ ){
            if(dis[j]>dis[u]+map[u][j]){
                dis[j] = dis[u] + map[u][j];
            }
        }
    }
}

int main(){
    while(~scanf("%d%d%d",&n,&m,&x)){
        memset(map,INF,sizeof(map));
        while(m--){
            scanf("%d%d%d",&a,&b,&time);
            map[a][b]=time;
        }
        dij();
        for( int i=1 ; i<=n ; i++ ){
            dd[i]=dis[i];
        }
        for( int i=1 ; i<=n ; i++ ){
            for( int j=i+1 ; j<=n ; j++ ){
                map[i][j] = map[i][j] ^ map[j][i];
                map[j][i] = map[i][j] ^ map[j][i];
                map[i][j] = map[i][j] ^ map[j][i];
            }
        }
        dij();
        int max=-1;
        for( int i=1 ; i<=n ; i++ ){
            if((dd[i]+dis[i])>max){
                max = dd[i]+dis[i];
            }
        }
        printf("%d\n",max);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/thesprit/article/details/51996659