poj 3268 Silver Cow Party(最短路)

来源:互联网 发布:php 接口验证 编辑:程序博客网 时间:2024/05/20 04:29

poj 3268 Silver Cow Party

Description
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

题目大意:有n个农场,m条路,并且在X号农场开party。现在在除了X号农场的其他农场,都有牛来X号农场开party,party结束以后就各回各家,当然,走的都是最短路。求这n - 1只牛中往返的最大的最短路。

解题思路:先求一遍正的以X为起点的最短路,然后把边反着读一遍,再求一遍以X为起点的最短路。把两次的d数组加起来,找最大值,就是答案。

#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>#include <queue>using namespace std;typedef long long ll;const int INF = 0x3f3f3f3f;const int N = 1005;const int M = 500005;int n, m, s;struct Edge {      int from, to, dist;  };  vector<Edge> edges, edgess;  vector<int> G[M], G2[M];  int vis[N], d[N], rec[N];int L[N];void init() {      for (int i = 0; i <= n; i++) {        L[i] = 0;        vis[i] = 0;    }    for (int i = 0; i < (n / 2) * n; i++) G[i].clear();      for (int i = 0; i < (n / 2) * n; i++) G2[i].clear();      edges.clear();      edgess.clear();}  void addEdge(int from, int to, int dist) {      edges.push_back((Edge){from, to, dist});      edgess.push_back((Edge){to, from, dist});    int pos = edges.size();      G2[to].push_back(pos - 1);      G[from].push_back(pos - 1);}  void input() {    int u, v, len;    for (int i = 0; i < m; i++) {        scanf("%d %d %d", &u, &v, &len);        addEdge(u, v, len);     }}void SPFA(vector<int> *GG, vector<Edge> e) {      memset(vis,0,sizeof(vis));      for (int i = 0; i < N; i++) d[i] = INF;    for (int i = 0; i < N; i++) rec[i] = INF;    d[s] = 0;      rec[s] = 0;      vis[s] = 1;      queue<int> Q;      Q.push(s);      while (!Q.empty()) {          int u = Q.front();          Q.pop();          vis[u] = 0;          for (int i = 0; i < GG[u].size(); i++) {              int v = e[GG[u][i]].to;              if (d[v] > d[u] + e[GG[u][i]].dist) {                  d[v] = d[u] + e[GG[u][i]].dist;                  rec[v] = rec[u] + 1;                  if (!vis[v]) {                      vis[v] = 1;                      Q.push(v);                  }              } else if (d[v] == d[u] + e[GG[u][i]].dist) {                  if (rec[v] > rec[u] + 1) {                      rec[v] = rec[u] + 1;                      if (!vis[v]) {                          vis[v] = 1;                          Q.push(v);                      }                  }              }          }      }  }  int main() {    int x, Max;    while (scanf("%d %d %d", &n, &m, &x) == 3) {        Max = 0;        init();             input();        s = x;        SPFA(G, edges);        for (int i = 1; i <= n; i++) {            if (i == x) continue;                   L[i] += d[i];        }        SPFA(G2, edgess);        for (int i = 1; i <= n; i++) {            if (i == x) continue;               L[i] += d[i];        }        for (int i = 1; i <= n; i++) {            Max = max(L[i], Max);        }        printf("%d\n", Max);    }    return 0;}
0 0