POJ

来源:互联网 发布:矩阵乘法 matlab 编辑:程序博客网 时间:2024/06/08 04:43
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.


题意:求1-n每个点到x的往返距离的最大值

思路:因为有2000ms,所以直接每个人dijkstra两次,再求最大值,比较简单,具体看代码


代码:

#include <cstdio>#include <cmath>#include <iostream>#include <cstring>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>#include <numeric>#include <set>#include <string>#include <cctype>#include <sstream>#define INF 0x3f3f3f3f#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1using namespace std;typedef long long LL;typedef pair<LL, LL> P;const int maxn = 1e3 + 5;const int mod = 1e8 + 7;int n, m, x;struct edge {    int to , cost;};vector<edge>G[maxn << 1];int d[maxn];void dijk(int s) {    priority_queue<P, vector<P>, greater<P> >q;    fill (d + 1, d + n + 1, INF);    d[s] = 0;    q.push(P(0, s));    while (!q.empty()) {        P p = q.top();        q.pop();        int v = p.second;        if (d[v] < p.first) continue;        for (int i = 0; i < G[v].size(); i++) {            edge e = G[v][i];            if (d[e.to] > d[v] + e.cost) {                d[e.to] = d[v] + e.cost;                q.push(P(d[e.to] , e.to));            }        }    }}int main() {    //freopen ("in.txt", "r", stdin);    while (~scanf ("%d%d%d", &n, &m, &x)) {        while (m--) {            int u, v, cost;            scanf ("%d%d%d", &u, &v, &cost);            G[u].push_back(edge{v, cost});        }        int Max = -1;        for (int i = 1; i <= n; i++) {            dijk(i);            int t = d[x];            dijk(x);            t += d[i];            Max = max(Max, t);        }        printf ("%d\n", Max);    }    return 0;}


原创粉丝点击