POJ3268 Silver Cow Party(最短路问题)

来源:互联网 发布:快速弄到钱网络偏门2万 编辑:程序博客网 时间:2024/05/17 09:05
Silver Cow Party
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 21730 Accepted: 9923

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: NM, and X 
Lines 2..M+1: Line i+1 describes road i with three space-separated integers: AiBi, 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 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.

Source

USACO 2007 February Silver

题目大意:一共有n个点,有一个点为X,且m条边都是单向边,问你从任何点到x加上x回到任何点的最短路里面,最长的多少

解题思路:这是一个最短路问题,由于从x回到各个点的路上是单源到其他点的最短路,所以可以用Bellman-Ford的算法,而各个点到x的路径可以用Ford算法,但是考虑到时间复杂度的问题,就改用反建图以此编程跟单源到其他点一模一样的了

#include<iostream>    #include<cstdio>  #include<stdio.h>  #include<cstring>    #include<cstdio>    #include<climits>    #include<cmath>   #include<vector>  #include <bitset>  #include<algorithm>    #include <queue>  #include<map>  using namespace std;#define inf 9999999;struct point{int x, k;}p;vector<point> tu[1005];queue<int> qua;vector<point> t[1005];long long int ans, b[1005], a[1005];int n, m, x, xx, y, i, k, j;bool check[1005];int main(){cin >> n >> m >> x;memset(a, 0, sizeof(a));for (i = 1; i <= n; i++){b[i] = inf;a[i] = inf;}b[x] = 0;a[x] = 0;for (i = 1; i <= m; i++){cin >> xx >> y >> k;p.x = y;p.k = k;tu[xx].push_back(p);p.x = xx;t[y].push_back(p);}qua.push(x);memset(check, 0, sizeof(check));while (!qua.empty()){k= qua.front();check[k] = false;qua.pop();for (i = 0; i < tu[k].size(); i++){p = tu[k][i];if (p.k + b[k] < b[p.x]){b[p.x] = p.k + b[k];if (check[p.x] != true){check[p.x] = true;qua.push(p.x);}}}}qua.push(x);memset(check, 0, sizeof(check));while (!qua.empty()){k = qua.front();check[k] = false;qua.pop();for (i = 0; i < t[k].size(); i++){p = t[k][i];if (p.k + a[k] < a[p.x]){a[p.x] = p.k + a[k];if (check[p.x] != true){check[p.x] = true;qua.push(p.x);}}}}ans = 0;for (i = 1; i <= n; i++){ans = max(ans,b[i] + a[i]);}cout << ans << endl;}


0 0