Farm Tour
Time limit:1000ms Memory limit:65536k
Total submissions:16367 accepted:6320
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 length.
Output
A single line containing the length of the shortest tour.
Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
Sample Output
6
Source
Usaco 2003 February Green
Analysis
Minimum cost maximum flow (expense flow) board ... But it takes some skill to build a map.
Test instructions: To a graph, from 1 to N, then from N to 1, and each side can only walk one side of the smallest side of the right.
Because each side can only walk once, so to each side set a flow of 1, the point 1 and the source point S connection, the cost is 0, the flow is 2, the N and the meeting point T connection, the cost is 0, the flow is 2.
Running on one side of the cost stream is fine.
Code
POJ 2135 Farm Tour #include <iostream> #include <cstring> #include <cstdio> #include <queue> #
Define INF 1e9+7 #define M (a) memset (a,0,sizeof a) #define FO (i,j,k) for (i=j;i<=k;i++) using namespace std;
const INT mxn=10005<<2;
Queue <int> q;
int N,m,s,t,cnt,ans;
BOOL VIS[MXN];
int head[1005],dis[1005],pre[1005];
struct node {int from,to,next,d,flow;} F[MXN]; inline void Add (int u,int v,int d,int flow) {F[++cnt].to=v,f[cnt].from=u,f[cnt].next=head[u],f[cnt].d=d,f[cnt].flow=f
low,head[u]=cnt;
f[++cnt].to=u,f[cnt].from=v,f[cnt].next=head[v],f[cnt].d=-d,f[cnt].flow=0,head[v]=cnt;
} inline void Spfa () {int i,u,v,d,flow;
memset (dis,0x3f,sizeof dis);
memset (pre,-1,sizeof Pre);
M (VIS);
Q.push (s);
dis[s]=0;
Vis[s]=1;
while (!q.empty ()) {U=q.front ();
Q.pop ();
vis[u]=0;
for (I=head[u];i;i=f[i].next) {v=f[i].to,d=f[i].d,flow=f[i].flow; if (dis[v]>dis[u]+d && flow>0) {dis[v]=dis[u]+d; Pre[v]=i;
Record precursor if (!vis[v]) Vis[v]=1,q.push (v);
}}}} inline void Maxflow () {int i,u,v,d;
SPFA ();
while (pre[t]!=-1) {int tmp=inf;
for (I=pre[t];i!=-1;i=pre[f[i].from]) tmp=min (Tmp,f[i].flow);
ans+=dis[t]*tmp;
for (I=pre[t];i!=-1;i=pre[f[i].from]) {f[i].flow-=tmp;
if (i&1) f[i+1].flow+=tmp;
else f[i-1].flow+=tmp;
} SPFA ();
}} int main () {int i,j,u,v,d;
scanf ("%d%d", &n,&m);
s=0,t=n+1;
while (m--) {scanf ("%d%d%d", &u,&v,&d);
Add (u,v,d,1); add (v,u,d,1);
} Add (s,1,0,2), add (1,s,0,2), add (n,t,0,2), add (t,n,0,2);
Maxflow ();
printf ("%d\n", ans);
return 0; }