This question and Cai big God out of this year Stoi Junior high School group's second question is almost identical ...
Run the shortest way first, then reverse all sides, run again, all points two times the maximum value of the addition is answer
-----------------------------------------------------------------------------
#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <iostream>#include <vector> #define REP (i, n) for (int i = 0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std;const INT MAXN = + 5;const int inf = 0X7FFFFFFF;typedef pair< int, int > edge;int cur = 0;int n; vector< Edge > e[2] [MAXN];
void Init () {
Rep (i, N)e[0] [i].clear (), e[1] [i].clear ();}inline void Add (int x, int u, int v, int d) {e[x [u].push_back (Make_pair (V, D));}inline void Add_edge (int u, int v, int d) {Add (0, U, V, d);Add (1, V, U, d);}bool inq[MAXN];int d[2 [MAXN];queue< int > Q;void SPFA (int S) {Rep (i, N)d[cur] [i] = inf;d[cur] [S] = 0; CLR (inQ, 0);While (! Q.empty ()) Q.pop ();Q.push (S);While (! Q.empty ()) {int x = Q.front ();Q.pop ();inq[x] = false;For (vector< edge >:: iterator E = e[cur [x].begin (); E! = e[cur] [x].end (); e++) {int to = first, dist = e-second;if (d[cur] [to] > d[cur [x] + dist) {d[cur] [to] = d[cur] [x] + dist;if (! inq[to])inq[to] = True, Q.push (to);}}}}int main () {//freopen ("test.in", "R", stdin);init ();int m, s;cin >> N >> m >> s;--s;While (m--) {int U, V, D;scanf ("%d%d%d", &u, &v, &d);u--, v--;Add_edge (U, V, d);}SPFA (s);cur ^= 1;SPFA (s);int ans = 0;Rep (i, N)ans = max (ans, d[0 [i] + d[1 [i]);cout << ans << "\ n";return 0;}
-----------------------------------------------------------------------------
1631: [Usaco2007 feb]cow Party Time limit: 5 Sec Memory Limit: + MB
Submit: 524 Solved: 388
[Submit] [Status] [Discuss] DescriptionThe Farm has N (1≤n≤1000) cattle stalls, each with 1 cows to participate in the X Cattle Cow party. Totalthere is M (1≤m≤100000) A one-way link to the barn, and the article I crippled need ti time to pass. Cows are lazy, so whether they go to the X-Barn party or return to their home, they use the least-used route. So how much time does it take to go back and forth with the biggest cows? Input
Line 1th: Three integers separated by a space.
Line 2nd to line m+1, three integers separated by spaces: Ai, Bi, and Ti. Represents the starting point of a road, the end point, and the time it takes.
Output
Unique line: An integer: The maximum amount of time that will be spent in all cows participating in the party.
Sample Input4 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 Output10
HINT
Sample Description:
There were 4 cows at the party, 8 Roads and a party on the 2nd farm.
The 4th cow can go directly to the party location (spending 3 time), then the return route passes through the 1th and 3rd farms (7 time), for a total of 10 hours.
Source
Silver
Bzoj 1631: [Usaco2007 Feb]cow Party (Shortest way)