1349 - Optimal Bus Route Design(二分图最小权完美匹配)
来源:互联网 发布:淘宝王子 删除中差评 编辑:程序博客网 时间:2024/05/22 02:26
该题是一道典型的二分图最小权完美匹配问题 。每个点恰好属于一个有向圈,意味着每个点都有一个唯一的后继 。某个东西恰好有唯一的.....这便是二分图匹配的特点 。
将每个结点拆成Xi和Yi,则原图中的有向边u->v对应二分图中的边Xu->Yv 。当流量满载时存在,存在完美匹配,否则不存在 。
网络流这类题目的难点在于将实际问题转化成理论模型,即建图过程 。 要想看出是二分图题目,就要明白这类题目的特点 。深入理解其匹配过程 。
细节参见代码:
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int maxn = 105*3;const ll INF = 1000000000;int n,m,u,v,c,b,t,p[maxn],a[maxn],inq[maxn],d[maxn];struct Edge { int from, to, cap, flow, cost; Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w) {}};vector<Edge> edges;vector<int> g[maxn];void init() { for(int i=0;i<maxn;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)); t = edges.size(); g[from].push_back(t-2); g[to].push_back(t-1);}bool BellmanFord(int s,int t,int& flow, ll& cost) { for(int i=0;i<maxn;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 += (ll)d[t] *(ll)a[t]; for(int u = t; u != s; u = edges[p[u]].from) { edges[p[u]].flow += a[t]; edges[p[u]^1].flow -= a[t]; } return true;}int MincostMaxflow(int s,int t, ll& cost) { int flow = 0; cost = 0; while(BellmanFord(s,t,flow,cost)) ; return flow;}int main() { while(~scanf("%d",&n)&&n) { init(); for(int i=1;i<=n;i++) { while(true) { scanf("%d",&b); if(b == 0) break; scanf("%d",&c); AddEdge(i,b+n,1,c); //拆分结点 } AddEdge(0,i,1,0); AddEdge(i+n,2*n+1,1,0); } ll cost; int flow = MincostMaxflow(0,2*n+1,cost); if(flow == n) printf("%lld\n",cost); else printf("N\n"); } return 0;}
1 0
- UVA 1349 Optimal Bus Route Design (最小费用流 -- 二分图最小权完美匹配)
- 1349 - Optimal Bus Route Design(二分图最小权完美匹配)
- UVA 1349 Optimal Bus Route Design(最小权完美匹配)
- uva 1349 - Optimal Bus Route Design(完美匹配)
- UVA 1349 Optimal Bus Route Design——最小权完美匹配
- UVa1349 Optimal Bus Route Design(二分图最佳完美匹配)
- Uvalive3353 Optimal Bus Route Design 带权二分图匹配
- UVA 1349 - Optimal Bus Route Design(KM完美匹配)
- uva1349 Optimal Bus Route Design 费用流,二分图匹配
- UVALive - 3353 Optimal Bus Route Design(二分图最佳匹配)
- LA 3353 Optimal Bus Route Design 二分匹配和有向图中的环
- (beginer) UVA 最小费用流 1349Optimal Bus Route Design
- uva-1349 Optimal Bus Route Design(最小费用最大流)
- UVA 1349Optimal 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
- 黑马程序员--Java基础学习(String)第十三天
- HDU 5417 Victor and Machine——BestCoder Round #52(div.2)
- Wiki——Windows下python2.7 爬虫框架Scrapy环境配置
- Debug Assertion Failed! File: afx.inl
- 115个Java面试题和答案——终极列表(下)
- 1349 - Optimal Bus Route Design(二分图最小权完美匹配)
- hdu3790 dijkstra算法
- HashTable 与HashMap 三点区别
- linux下配置jdk环境变量的三种方法
- OpenGL之坐标转换(好文-清晰版)
- storm shuffleGrouping随机分组
- Android Api Demos登顶之路(四十四)Launcher Shortcuts
- HDU 1035.Robot Motion【还是模拟】【8月22】
- 图论 - hdu5348 MZL's endless loop