Farm tour
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:11495 |
|
Accepted:4257 |
Description
When FJ's friends visit him on the farm, he likes to show them around. his farm comprises N (1 <= n <= 1000) fields numbered 1 .. n, the first of which contains his house and the nth of which contains the big barn. A total m (1 <= m <= 10000) paths that connect the fields in various ways. each path connects two different fields and has a nonzero length smaller than 35,000.
To show off his farm in the best way, he walks a tour that starts at his house, potentially travels through some fields, and ends at the barn. later, he returns (potentially through some fields) back to his house again.
He wants his tour to be as short as possible, however he doesn' t want to walk on any given path more than once. calculate the shortest tour possible. FJ is sure that some tour exists for any given farm.
Input
* Line 1: two space-separated integers: N and M.
* Lines 2.. m + 1: three space-separated integers that define a path: the starting field, the end field, and the path's length.
Output
A single line containing the length of the shortest tour.
Sample Input
4 51 2 12 3 13 4 11 3 22 4 2
Sample output
6
From to, and then go back to. Each route is required and will not be taken twice. Because the road is undirected, you need to add two sides, convert the rotation to take two steps from 1 to n to obtain the minimum cost.
# Include <cstdio> # include <cstring> # include <queue> # include <algorithm> using namespace STD; # define maxn 11000 # define INF 0x3f3f3f # define ll _ int64struct node {ll V, W, s; ll next;} p [maxn * 30]; ll head [maxn], CNT, vis [maxn], pre [maxn], DIS [maxn]; queue <ll> q; void add (ll u, ll V, ll W, ll s) {P [CNT]. V = V; P [CNT]. W = W; P [CNT]. S = s; P [CNT]. next = head [u]; head [u] = CNT ++; P [CNT]. V = u; P [CNT]. W = 0; P [CNT]. S =-S; P [CNT]. next = head [v]; head [v] = CNT ++;} void spfa (ll s, ll t) {ll U, V, I; memset (PRE, -1, sizeof (pre); memset (DIS, INF, sizeof (DIS); memset (VIS, 0, sizeof (VIS); While (! Q. Empty () Q. Pop (); q. Push (s); vis [s] = 1; DIS [s] = 0; while (! Q. Empty () {u = Q. Front (); q. Pop (); vis [u] = 0; for (I = head [u]; I! =-1; I = P [I]. next) {v = P [I]. v; If (P [I]. W & dis [v]> dis [u] + P [I]. s) {dis [v] = dis [u] + P [I]. s; Pre [v] = I; If (! Vis [v]) {vis [v] = 1; q. push (v) ;}}}} void F (ll s, ll t) {ll I, j, m = 2, ANS = 0, min1; while (M --) {spfa (S, T); min1 = inf; for (I = pre [T]; I! =-1; I = pre [p [I ^ 1]. v]) if (P [I]. W <min1) min1 = P [I]. w; for (I = pre [T]; I! =-1; I = pre [p [I ^ 1]. v]) {P [I]. w-= min1; P [I ^ 1]. W + = min1; ans + = P [I]. s ;}} printf ("% i64d \ n", ANS);} int main () {ll n, m, U, V, S; while (scanf ("% i64d % i64d", & N, & M )! = EOF) {/* go back from to. Because the line is undirected, You have to build it twice. The same side can only go once, break it back from to and break it down into two steps from 1 to n */memset (Head,-1, sizeof (head); CNT = 0; while (M --) {scanf ("% i64d % i64d % i64d", & U, & V, & S); add (u, v, 1, S ); add (v, U, 1, S) ;}f (1, N); // a billing flow template...} Return 0 ;}