POJ-3268 Silver Cow Party

来源:互联网 发布:vr飞机杯软件 编辑:程序博客网 时间:2024/04/30 13:31
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536K

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 ≤XN). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roadi 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 farmAi 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 21 2 41 3 21 4 72 1 12 3 53 1 23 4 44 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.
————————————————————集训13.4的分割线————————————————————
前言:跟POJ-1511 Invitation Cards
一个思路。
思路:建立两张图,一张是正的,另一张是反的。所以源点仅有X点,跑两次SPFA。最后维护一下答案。找最大的最小值之和。
代码如下:
/* ID: j.sure.1 PROG:  LANG: C++ */  /****************************************/  #include <cstdio>  #include <cstdlib>  #include <cstring>  #include <algorithm>  #include <cmath>  #include <stack>  #include <queue>  #include <vector>  #include <map>  #include <string>  #include <iostream>  #include <climits>  #define INF 0x3f3f3f3f  using namespace std;  /****************************************/    const int N = 1005, M = 1e5+5;  int n, m, tot, head[N], ophead[N],  dis[2][N];  bool inq[N];  struct Node {      int u, v, w;      int next;  }edge[M], opedge[M];    void init()  {      tot = 0;      memset(head, -1, sizeof(head));      memset(ophead, -1, sizeof(ophead));  }    void add(int u, int v, int w){      edge[tot].u = u; edge[tot].v = v; edge[tot].w = w;      edge[tot].next = head[u]; head[u] = tot;      opedge[tot].u = v; opedge[tot].v = u; opedge[tot].w = w;      opedge[tot].next = ophead[v]; ophead[v] = tot;      tot++;  }    void spfa(int st, int (&hh)[N], Node (&Edge)[M], int id)//C++引用数组的方法,避免降值问题的发生  {for(int i = 0; i <= n; i++)dis[id][i] = INF;    memset(inq, 0, sizeof(inq));      queue <int> q;//局部队列,不需清空      q.push(st);      dis[id][st] = 0;      inq[st] = true;      while(!q.empty()) {          int u = q.front();          q.pop();          inq[u] = false;          for(int i = hh[u]; i != -1; i = Edge[i].next) {              int v = Edge[i].v;              if(dis[id][v] > dis[id][u] + Edge[i].w) {//松弛操作                  dis[id][v] = dis[id][u] + Edge[i].w;                  if(!inq[v]) {                      q.push(v);                      inq[v] = true;                  }              }          }      }  }    int main()  {  #ifdef J_Sure      freopen("000.in", "r", stdin);  //  freopen(".out", "w", stdout);  #endif     init();int st;scanf("%d%d%d", &n, &m, &st);  int a, b, c;  for(int i = 0; i < m; i++) {  scanf("%d%d%d", &a, &b, &c);  add(a, b, c);}  int ans = 0;  spfa(st, head, edge, 0);  spfa(st, ophead, opedge, 1);  int maxi = -1;for(int i = 2; i <= n; i++) {  maxi = max(maxi, dis[0][i] + dis[1][i]);  }  printf("%d\n", maxi);//正反各跑一次,统计答案           return 0;    } 


0 0
原创粉丝点击