UVA 1349 Optimal Bus Route Design(最小权完美匹配)
来源:互联网 发布:mac 流程图软件 免费 编辑:程序博客网 时间:2024/05/16 05:03
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4095
思路:要求每个点属于且仅属于一个圈,则由于在一个圈中的每个点必定存在后继,又由于每个点只能属于一个圈,所以每个点有且仅有一个后继时满足题意。联想到匹配问题,则对于每条边<i,j>转化为连边<i,j+n>,权值为该边权值,构成一新图。此图为一二分图,则最小费用即为最小权完美匹配值。可用最小费用流解决,所以,设一源点s,汇点t,从s到每个i连边,容量为1,费用为0,;每个j向t连边,容量为1,费用为0;对于i、j,连边,容量为1,费用为该边权值。则此时最小权完美匹配值即为最小费用流,若从s出发的边与汇入t的边均满流,则存在完美匹配,否则不存在,无解。
#include<cstdio>#include<queue>#include<vector>#include<cstring>#include<iostream>#include<algorithm>#define debuusing namespace std;const int maxn=200+50;const int INF=0x3f3f3f3f;struct Edge{ int from,to,cap,flow,cost;};struct MCMF{ int n,m,s,t; vector<Edge> edges; vector<int> G[maxn]; int inq[maxn],d[maxn]; int p[maxn],a[maxn]; void init(int n) { this->n=n; for(int i=0; i<n; i++) G[i].clear(); edges.clear(); } void AddEdge(int from,int to,int cap,int cost) { edges.push_back((Edge){from,to,cap,0,cost}); edges.push_back((Edge){to,from,0,0,-cost}); m=edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BellmanFord(int s,int t,int &flow,int &cost) { for(int i=0; i<n; i++) d[i]=INF; memset(inq,0,sizeof(inq)); d[s]=0,inq[s]=1,p[s]=0,a[s]=INF; queue<int> Q; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(),inq[u]=0; for(int i=0; i<G[u].size(); i++) { Edge &e=edges[G[u][i]]; if(e.cap>e.flow&&d[e.to]>d[u]+e.cost) { d[e.to]=d[u]+e.cost; p[e.to]=G[u][i]; a[e.to]=min(a[u],e.cap-e.flow); if(!inq[e.to]) { Q.push(e.to); inq[e.to]=1; } } } } if(d[t]==INF) return false; flow+=a[t]; cost+=d[t]*a[t]; int u=t; while(u!=s) { edges[p[u]].flow+=a[t]; edges[p[u]^1].flow-=a[t]; u=edges[p[u]].from; } return true; } int Mincost(int s,int t) { int flow=0,cost=0; while(BellmanFord(s,t,flow,cost)); return cost; }};int n;MCMF G;int main(){#ifdef debug freopen("in.in","r",stdin);#endif // debug while(scanf("%d",&n)!=EOF&&n) { int tmp[150]; G.init(2*n+2); for(int i=1; i<=n; i++) { int x,w; G.AddEdge(0,i,1,0); G.AddEdge(i+n,2*n+1,1,0); tmp[i]=G.m-2; while(scanf("%d",&x)!=EOF&&x) { scanf("%d",&w); G.AddEdge(i,x+n,1,w); } } int flag=1,ans=G.Mincost(0,2*n+1); for(int i=0; i<G.G[0].size(); i++) { Edge &e=G.edges[G.G[0][i]]; if(e.flow!=1) { flag=0; break; } } for(int i=1; i<=n; i++) { Edge &e=G.edges[tmp[i]]; if(e.flow!=1) { flag=0; break; } } if(!flag) printf("N\n"); else printf("%d\n",ans); } return 0;}
0 0
- UVA 1349 Optimal Bus Route Design (最小费用流 -- 二分图最小权完美匹配)
- UVA 1349 Optimal Bus Route Design(最小权完美匹配)
- uva 1349 - Optimal Bus Route Design(完美匹配)
- UVA 1349 Optimal Bus Route Design——最小权完美匹配
- 1349 - Optimal Bus Route Design(二分图最小权完美匹配)
- UVA 1349 - Optimal Bus Route Design(KM完美匹配)
- (beginer) UVA 最小费用流 1349Optimal Bus Route Design
- uva-1349 Optimal Bus Route Design(最小费用最大流)
- Uva-1349-Optimal Bus Route Design
- UVa 1349 Optimal Bus Route Design
- UVA 1349 Optimal Bus Route Design
- Optimal Bus Route Design UVA 1349
- UVA 1349Optimal Bus Route Design(网络流)
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
- UVA 1349 Optimal Bus Route Design 最小费用流+拆点
- Uvalive3353 Optimal Bus Route Design 带权二分图匹配
- UVa #1349 Optimal Bus Route Design (例题11-10)
- UVA 1349 Optimal Bus Route Design(KM判环)
- RxJava学习总结4
- 1051. 复数乘法 (15)
- leecode 解题总结:59. Spiral Matrix II
- CSU-1262
- Putty使用密钥进行自动登录的一些经验
- UVA 1349 Optimal Bus Route Design(最小权完美匹配)
- M--二分查找(递归)
- 百度地图 定位
- 1052. 卖个萌 (20)
- 第一篇博客
- iOS中NSDate常用转换操作整合
- 蓝桥杯之趣味算式
- Android插件化系列第(二)篇---动态加载技术之应用换肤
- linux 下mysql数据的导入、导出