Farm Tour
Time Limit: 2MS |
|
Memory Limit: 65536KB |
|
64bit IO Format: %i64d &%i64u |
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 POJ the corresponding page suddenly can't get in, can't evaluate. Currently only the sample minimum cost issue is ensured
1 /*by Silvern*/2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <queue>8 using namespacestd;9 Const intinf=1000000;Ten Const intmxn=2100; One intHEAD[MXN],DIS[MXN],PR[MXN]; A BOOLINQU[MXN]; - intn,m; - ints,t; the intans; - intCnt=1; - structedge{ - int from, To,next,v,c; +}e[mxn* -]; - voidAdd_edge (intFintTintVintc) { +E[++cnt]= (Edge) {f,t,head[f],v,c};head[f]=CNT; AE[++cnt]= (Edge) {t,f,head[t],0,-c};head[t]=CNT; at } - BOOLSPFA () { -queue<int>Q; -memset (Inqu,false,sizeof(Inqu)); - for(intI=0; i<=t;i++) dis[i]=INF; -dis[s]=0; ininqu[s]=1; - Q.push (s); to inti,j; + while(!Q.empty ()) { - intu=Q.front (); Q.pop (); theinqu[u]=false; * for(i=head[u];i;i=E[i].next) { $ intv=e[i].to;Panax Notoginseng if(E[I].V && dis[u]+e[i].c<Dis[v]) { -dis[v]=dis[u]+e[i].c; thepr[v]=i; + if(!Inqu[v]) { A Q.push (v); theinqu[v]=true; + } - } $ } $ } - returndis[t]!=INF; - } the voidmcf () { - inti;Wuyi while(SPFA ()) { the inttemp=INF; - for(I=pr[t];i;i=e[pr[i]]. from) temp=min (temp,e[i].v); Wuans+=dis[t]*temp; - for(I=pr[t];i;i=e[pr[i]]. from){ Aboute[i].v-=temp; $e[i^1].v+=temp; - } - } - } A intMain () { +scanf"%d%d",&n,&m); thes=0; t=n+1; - intx,y,c; $ for(intI=1; i<=m;i++){ thescanf"%d%d%d",&x,&y,&c); theAdd_edge (x, Y,1, c); theAdd_edge (Y,x,1, c); the } -Add_edge (s),1,2,0); inAdd_edge (N,t,2,0); the MCF (); theprintf"%d\n", ans); About}
POJ2135 Farm Tour