"Luogu 1821" [USACO07FEB] Silver Bull Party Silver Cow Party

Better reading experience

Portal

Portal1: Luogu

Portal2: CONCEPT

Description

One cow from each of N farms \((1 \le N \le 1000)\) conveniently numbered \(1 \cdots N\) is going to attend the big cow party to be held at farm #X \((1 \le X \le N)\). A total of \(M (1 \le M \le 100,000)\) unidirectional (one-way roads connects pairs of farms; road \(i\) requires \(T_i (1 \le Ti \le 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?

To the winter, \ (N \) cows have to attend a numbered \ (the X-\) ( \ (1 \ the X-Le \ Le N \) party held) of farm cattle ( \ (1 \ Le N \ Le 1000 \) ), there is between the farm \ (M \) ( \ (. 1 \ Le M \ 100000 Le \) ) has the road section, each path length \ (of Ti \) ( \ (. 1 \ Le Ti \ Le 100 \) ).

After each cow attending the party must go home, either to the party or go home, each cow will choose the shortest path, shortest path for the N cow (back and forth) in the longest path length.

Input

The first row of three integers \ (N \) , \ (M \) , \ (X-\) ;

The second row of the \ (M + 1 \) line: each line has three integers \ (A_i \) , \ (B_i \) , \ (T_i \) , expressed from a \ (A_i \) farm to \ ( b_i \) road farm, length \ (T_i \) .

Output

An integer representing the length of the longest shortest path available.

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

Solution

Let some of the cows title come to a point, and then go from that point back and the shortest of the maximum. So we directly dijkstracalculate the shortest path twice (walk, walk back) on it, and finally determine what, cows need to take the other end of the road is the longest, then the problem is solved.

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>

using namespace std;

const int INF = 0x3f3f3f3f, MAXN = 200005;
struct EDGE {
    int nxt, to, val;
} edge[MAXN];
int n, m, S, cnt, U[MAXN], V[MAXN], VAL[MAXN], dis[MAXN], dist[MAXN], head[MAXN];
bool vis[MAXN];
inline void addedge(int u, int v, int val) {//邻接表存图
    edge[++cnt].to = v; edge[cnt].val = val; edge[cnt].nxt = head[u]; head[u] = cnt;
}
inline void dijkstra(int S) {//dijkstra最短路
    memset(dis, INF, sizeof(dis));
    priority_queue< pair<int, int> > Q;
    Q.push(make_pair(0, S));
    dis[S] = 0;
    while (!Q.empty()) {
        int u = Q.top().second;
        Q.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = head[u]; ~i; i = edge[i].nxt) {
            int v = edge[i].to;
            if (dis[v] > dis[u] + edge[i].val) {
                dis[v] = dis[u] + edge[i].val;
                Q.push(make_pair(-dis[v], v));
            }
        }
    }
}
int main() {
    scanf("%d%d%d", &n, &m, &S);
    memset(head, -1, sizeof(head));
    for (int i = 1; i <= m; i++) {
        scanf("%d%d%d", &U[i], &V[i], &VAL[i]);
        addedge(U[i], V[i], VAL[i]);//正向建图
    }
    dijkstra(S);
    for (int i = 1; i <= n; i++)
        dist[i] = dis[i];//记录走到目标点的路程
    cnt = 0;
    memset(edge, 0, sizeof(edge));
    memset(vis, 0, sizeof(vis));
    memset(head, -1, sizeof(head));//注意清空数组
    for (int i = 1; i <= m; i++)
        addedge(V[i], U[i], VAL[i]);//反向建图
    dijkstra(S);
    int Max = -INF;
    for (int i = 1; i <= n; i++)
        Max = max(Max, dis[i] + dist[i]);//判断那个奶牛是走得最多的
    printf("%d\n", Max);
    return 0;
}

Guess you like

Origin www.cnblogs.com/shenxiaohuang/p/11594332.html