[POJ 2135] Farm Tour

Farm Tour
TL: 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 <= 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 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2

Sample Output

6

Source

【题解】
最小费用可行流
要去要回,就相当于从S到T流流量为2的流,求mincost
所有边流量为1,S->1,n->T流量为2
来一遍最小费用流即可。
数组不够大,POJ WA了几发TAT
 1 #include <stdio.h>
 2 #include <string.h> 
 3 #include <queue>
 4 using namespace std;
 5 const int B=50010;
 6 int tot, head[B], next[B], flow[B], w[B], to[B], d[B], S, T, pre[B], bi[B], oq[B];
 7 bool vis[B];
 8 int n,m;
 9 queue<int> q;
10 inline void add(int u,int v,int _w,int _flow) {
11     to[tot]=v; flow[tot]=_flow; w[tot]=_w; next[tot]=head[u]; head[u]=tot++;
12     to[tot]=u; flow[tot]=0; w[tot]=-_w; next[tot]=head[v]; head[v]=tot++;    
13 }
14 inline int spfa() {
15     memset(d,0x3f3f3f3f,sizeof(d));
16     memset(oq,0,sizeof(oq));
17     memset(vis,0,sizeof(vis));
18     memset(pre,-1,sizeof(pre));
19     memset(bi,0,sizeof(bi));
20     while(!q.empty()) q.pop();
21     d[S]=0;
22     vis[S]=1;
23     pre[S]=S;
24     q.push(S);
25     while(!q.empty()) {
26         int top=q.front();
27         q.pop();
28         vis[top]=0;
29         oq[top]++;
30         if(oq[top]>n+2) return -1;
31         for (int i=head[top];i!=-1;i=next[i]) 
32             if(flow[i]&&d[top]+w[i]<d[to[i]]) {
33                 d[to[i]]=d[top]+w[i];
34                 if(!vis[to[i]]) {
35                     vis[to[i]]=1;
36                     q.push(to[i]);
37                 }
38                 pre[to[i]]=top;
39                 bi[to[i]]=i;
40             }
41     }
42     if(d[T]==0x3f3f3f3f) return -1;
43     else return d[T];
44 }
45 inline int mincostflow(int S,int T) {
46     int flowx, cost;
47     flowx=cost=0;
48     while(1) {
49         int minf=0x3f3f3f3f;
50         int Spfa = spfa();
51         if (Spfa==-1||Spfa==0x3f3f3f3f) break;
52         for (int i=T;i!=S;i=pre[i])
53             if (flow[bi[i]]<minf) minf=flow[bi[i]];
54         flowx += minf;
55         cost += Spfa*minf;
56         for (int i=T;i!=S;i=pre[i]) {
57             flow[bi[i]]-=minf;
58             flow[bi[i]^1]+=minf;
59         }
60         //printf("--%d %d
",flowx,cost);
61     }
62     return cost;
63 }
64 
65 int main() {
66     memset(head,-1,sizeof(head));
67     scanf("%d%d",&n,&m);
68     for (int i=1;i<=m;++i) {
69         int _u,_v,_w;
70         scanf("%d%d%d",&_u,&_v,&_w);
71         add(_u,_v,_w,1);
72         add(_v,_u,_w,1);
73     }
74     // 添加源和汇 
75     S=0, T=n+1;
76     add(S,1,0,2);
77     add(n,T,0,2);
78     printf("%d
",mincostflow(S,T)); 
79     return 0;
80 }
View Code
原文地址:https://www.cnblogs.com/TonyNeal/p/poj2135.html