Farm Tour(最小费用最大流模板)
Posted happy_code
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Farm Tour(最小费用最大流模板)相关的知识,希望对你有一定的参考价值。
Farm Tour
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18150 | Accepted: 7023 |
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.
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.
* 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
//题意:n 个点, m 条无向边,要求从 1 走到 n 点,再从 n 走到 1 点,不能走重复路,求最短路径
//可以看成从 1 走到 n 用两种路径,因为只能走一遍,所以边的容量为 1
所以建立一个附加的源点容量为2,费用为 0 到1点,附加的汇点容量为 2 ,费用为 0 到 n+1 点跑最小费用最大流即可
1 //# include <bits/stdc++.h> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 # define eps 1e-8 8 # define INF 0x3f3f3f3f 9 # define pi acos(-1.0) 10 # define MXN 1005 11 # define MXM 20050 12 13 struct Edge{ 14 int from, to, flow, cost, cap; 15 }edges[MXM*2]; //有向边数*2 16 17 struct MCMF{ 18 int n, m, idx; //点,边,边数 19 int flow, cost; 20 vector<int> G[MXN]; //记录边 21 int inq[MXN]; //BFS用 22 int dis[MXN]; //层次 23 int pre[MXN]; //上一条弧 24 int adf[MXN]; //增量 25 26 void Init(){ 27 idx=0; 28 for (int i=0;i<=n+1;i++) G[i].clear(); //有附加点时要注意 29 } 30 31 void Addedge(int u,int v,int cost,int cap){ 32 edges[idx++] = (Edge){u,v,0,cost,cap}; 33 edges[idx++] = (Edge){v,u,0,-cost,0}; 34 G[u].push_back(idx-2); 35 G[v].push_back(idx-1); 36 } 37 38 int Bellman(int s, int t) 39 { 40 memset(dis,0x3f,sizeof(dis)); //有附加点时要注意 41 memset(inq,0,sizeof(inq)); 42 dis[s]=0, inq[s]=1, adf[s]=INF; 43 queue<int> Q; 44 Q.push(s); 45 while (!Q.empty()) 46 { 47 int u = Q.front(); Q.pop(); 48 inq[u]=0; 49 for (int i=0;i<(int)G[u].size();i++) 50 { 51 Edge &e = edges[G[u][i]]; 52 if (dis[e.to] > dis[u] + e.cost && e.cap > e.flow) 53 { 54 dis[e.to] = dis[u] + e.cost; 55 adf[e.to] = min(adf[u], e.cap-e.flow); 56 pre[e.to] = G[u][i]; 57 if (!inq[e.to]) 58 { 59 Q.push(e.to); 60 inq[e.to]=1; 61 } 62 } 63 } 64 } 65 if (dis[t]==INF) return false; 66 67 flow+=adf[t]; 68 cost+=adf[t]*dis[t]; 69 int x=t; 70 while(x!=s) 71 { 72 edges[pre[x]].flow+=adf[t]; 73 edges[pre[x]^1].flow-=adf[t]; 74 x=edges[pre[x]].from; 75 } 76 return true; 77 } 78 79 int MinCost(int s,int t) 80 { 81 flow = 0, cost = 0; 82 while(Bellman(s, t)); 83 return cost; 84 } 85 }F; 86 87 int main() 88 { 89 scanf("%d%d",&F.n,&F.m); 90 F.Init(); 91 for (int i=1;i<=F.m;i++) 92 { 93 int u,v,cost; 94 scanf("%d%d%d",&u,&v,&cost); 95 F.Addedge(u,v,cost,1); 96 F.Addedge(v,u,cost,1); 97 } 98 F.Addedge(0, 1, 0, 2); 99 F.Addedge(F.n, F.n+1, 0, 2); 100 printf("%d\n",F.MinCost(0,F.n+1)); 101 return 0; 102 }
以上是关于Farm Tour(最小费用最大流模板)的主要内容,如果未能解决你的问题,请参考以下文章