Farm Tourtl:1000ms, ML:32MB
Description
When FJ's friends visit him on the farm, he likes to show them around. His farm comprises N (1 <= n <=) fields numbered 1..N, the first of which contains he house and the Nth of WHI CH contains the Big barn. A Total M (1 <= m <= 10000) Paths this connect the fields in various ways. Each path connects the different fields and have a nonzero length smaller than 35,000.
To show off his farm in the best-of-the-walks, he-a-tour-that-starts at his house, potentially travels through-some fields, a nd ends at the barn. Later, he returns (potentially through some fields) the back to his house again.
He wants his tour to being as short as possible, however he doesn ' t want-walk on any given path more than once. Calculate the shortest tour possible. FJ is sure this 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 lengt H.
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
Source
Usaco 2003 February Green" The Puzzle"minimum cost feasible flowTo go back, it is equivalent to a stream from S to t flow of 2, seeking MincostAll edge traffic is 1,s->1,n->t flow is 2go through the minimum cost flow. the array is not big enough, POJ wa a few tat
1#include <stdio.h>2#include <string.h>3#include <queue>4 using namespacestd;5 Const intb=50010;6 inttot, head[b], next[b], flow[b], w[b], to[b], d[b], S, T, Pre[b], bi[b], oq[b];7 BOOLVis[b];8 intn,m;9queue<int>Q;TenInlinevoidAddintUintVint_w,int_flow) { OneTo[tot]=v; Flow[tot]=_flow; W[tot]=_w; Next[tot]=head[u]; head[u]=tot++; ATo[tot]=u; flow[tot]=0; W[tot]=-_w; NEXT[TOT]=HEAD[V]; head[v]=tot++; - } -InlineintSPFA () { thememset (D,0x3f3f3f3f,sizeof(d)); -memset (OQ,0,sizeof(OQ)); -memset (Vis,0,sizeof(Vis)); -memset (pre,-1,sizeof(pre)); +memset (BI,0,sizeof(BI)); - while(!q.empty ()) Q.pop (); +d[s]=0; Avis[s]=1; atpre[s]=S; - Q.push (S); - while(!Q.empty ()) { - inttop=Q.front (); - Q.pop (); -vis[top]=0; inoq[top]++; - if(oq[top]>n+2)return-1; to for(inti=head[top];i!=-1; i=Next[i]) + if(flow[i]&&d[top]+w[i]<D[to[i]]) { -d[to[i]]=d[top]+W[i]; the if(!Vis[to[i]]) { *vis[to[i]]=1; $ Q.push (To[i]);Panax Notoginseng } -pre[to[i]]=top; thebi[to[i]]=i; + } A } the if(d[t]==0x3f3f3f3f)return-1; + Else returnD[t]; - } $InlineintMincostflow (intSintT) { $ intflowx, cost; -flowx=cost=0; - while(1) { the intminf=0x3f3f3f3f; - intSPFA =SPFA ();Wuyi if(spfa==-1|| spfa==0x3f3f3f3f) Break; the for(intI=t;i!=s;i=Pre[i]) - if(Flow[bi[i]]<minf) minf=Flow[bi[i]]; WuFlowx + =Minf; -Cost + = spfa*Minf; About for(intI=t;i!=s;i=Pre[i]) { $flow[bi[i]]-=Minf; -flow[bi[i]^1]+=Minf; - } - //printf ("--%d%d\n", flowx,cost); A } + returnCost ; the } - $ intMain () { thememset (head,-1,sizeof(head)); thescanf"%d%d",&n,&m); the for(intI=1; i<=m;++i) { the int_u,_v,_w; -scanf"%d%d%d",&_u,&_v,&_w); inAdd (_u,_v,_w,1); theAdd (_v,_u,_w,1); the } About //add source and sink thes=0, t=n+1; theAdd (S,1,0,2); theAdd (N,t,0,2); +printf"%d\n", Mincostflow (s,t)); - return 0; the}
View Code
[POJ 2135] Farm Tour