Farm Tour POJ - 2135

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

题解:放弃把问题当作去去和回的这种想法,转而将问题当作求从1号顶点到n号顶点的两条没有公共边的路径,这样转化之后,就不过是求流量为2的

最小费用流了。--------------------------------------摘自《挑战程序设计竞赛》

 1 #include<queue>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 const int INF=1e8;
 9 const int maxv=1005;
10 
11 struct edge{ int to,cap,cost,rev; };
12 
13 int n,m,V;
14 int d[maxv],prevv[maxv],preve[maxv];
15 vector<edge> G[maxv];
16 
17 void addedge(int from,int to,int cap,int cost){
18     G[from].push_back((edge){to,cap,cost,G[to].size()});
19     G[to].push_back((edge){from,0,-cost,G[from].size()-1});
20 } 
21 
22 int min_cost_flow(int s,int t,int f){
23     int ans=0;
24     while(f>0){
25         fill(d,d+V+1,INF);
26         d[s]=0;
27         bool update=true;
28         while(update){
29             update=false;
30             for(int v=1;v<=V;v++){
31                 if(d[v]==INF) continue;
32                 for(int i=0;i<G[v].size();i++){
33                     edge& e=G[v][i];
34                     if(e.cap>0&&d[e.to]>d[v]+e.cost){
35                         d[e.to]=d[v]+e.cost;
36                         prevv[e.to]=v;
37                         preve[e.to]=i;
38                         update=true;
39                     }
40                 }
41             }
42         }
43         if(d[t]==INF) return -1;
44         int md=f;
45         for(int v=t;v!=s;v=prevv[v]) md=min(md,G[prevv[v]][preve[v]].cap);
46         f=f-md;
47         ans=ans+md*d[t];
48         for(int v=t;v!=s;v=prevv[v]){
49             edge& e=G[prevv[v]][preve[v]];
50             e.cap-=md;
51             G[v][e.rev].cap+=md;
52         }
53     }
54     return ans;
55 }
56 
57 void solve(){
58     int s=1,t=n;
59     V=n;
60     for(int i=0;i<=n+1;i++) G[i].clear();
61     for(int i=0;i<m;i++){
62         int u,v,c;
63         scanf("%d%d%d",&u,&v,&c);
64         addedge(u,v,1,c);
65         addedge(v,u,1,c);
66     }
67     cout<<min_cost_flow(s,t,2)<<endl;
68 }
69 
70 int main()
71 {   cin>>n>>m;
72     solve();
73     return 0;
74 }

 

posted @ 2017-08-21 17:35  天之道,利而不害  阅读(147)  评论(0)    收藏  举报