Silver Cow Party 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 ≤ 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:  NM, 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牛出发,向所有的牛走去,找最短路。

在反向建边,在跑一边最短路,两次相加就可以了,

然后一边for循环,找出最大值。

扫描二维码关注公众号,回复: 571507 查看本文章


#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5+10;

int N,M,X,sign = 0;
int head[2000],rehead[2000];
int dis[2000],diss[2000];
bool vis[2000];
struct node{
    int next;
    int v,w;
}edge[MAX_N],reedge[MAX_N];

void add_edge(int x,int y,int z){   //这个就是链式前向星,邻接表吧 。
    sign++;
    edge[sign].next = head[x];
    edge[sign].v = y;
    edge[sign].w = z;
    head[x] = sign;
    reedge[sign].next = rehead[y];
    reedge[sign].v = x;
    reedge[sign].w = z;
    rehead[y] = sign;
}

void spfa(){
    memset(dis,0x3f3f3f3f,sizeof(dis));
    dis[X] = 0;
    memset(vis,0,sizeof(vis));
    vis[X] = 1;
    queue<int>q;
    q.push(X);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = head[u]; i != 0; i = edge[i].next){
            int v = edge[i].v,w = edge[i].w;
            if (dis[v] > dis[u] + w)
            {
                dis[v] = dis[u] + w;
                if (vis[v] == 0){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return;
}

void spfaa(){
    memset(diss,0x3f3f3f3f,sizeof(diss));
    diss[X] = 0;
    memset(vis,0,sizeof(vis));
    vis[X] = 1;
    queue<int>q;
    q.push(X);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = rehead[u]; i != 0; i = reedge[i].next){
            int v = reedge[i].v,w = reedge[i].w;
            if (diss[v] > diss[u] + w) {
                diss[v] = diss[u] + w;
                if (vis[v] == 0){
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }

    }
    return;
}
int main() {
    int x,y,z;
    scanf("%d%d%d",&N,&M,&X);
    for (int i = 0; i < M; i++){
        scanf("%d%d%d",&x,&y,&z);
        add_edge(x,y,z);
    }
    spfa();
    spfaa();
    int sum = 0;
    for (int i = 1; i <= N; i++)
    if (diss[i] + dis[i] > sum) sum = dis[i] + diss[i];
    printf("%d\n",sum);
        return 0;
}


猜你喜欢

转载自blog.csdn.net/kidsummer/article/details/80255892