网络流24题18. 分配问题
2017-03-07 09:55
489 查看
分配问题
Description
有 n 件工作要分配给 n 个人做。第 i 个人做第 j 件工作产生的效益为cij。试设计一个将n 件工作分配给 n 个人做的分配方案,使产生的总效益最大。对于给定的 n 件工作和 n 个人,计算最优分配方案和最差分配方案。
Input
第 1 行有 1 个正整数 n,表示有 n 件工作要分配给 n 个人做。接下来的 n 行中,每行有 n 个整数cij,1≤i≤n,1≤j≤n,表示第 i 个人做第 j 件工作产生的效益为cij。Output
输出计算出的最小总效益和最大总效益。题解
二分图匹配问题。建立附加源S,附加汇T。
建图:
1.S向每个人连接一条容量为1,费用为0的边。
2.每件工作向T连接一条容量为1,费用为0的边。
3.每个人向每件工作连接一条容量为1,费用为cij。
答案即为最小(大)费用最大流。
#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int N = 1000 + 10, M = 1000000 + 10, inf = 0x3f3f3f3f; struct Edge{ int fr, to, cap, flow, cost; }edg[M]; int hd , nxt[M], tot; int n, s, t; int d , a , p , q , inq ; int mp[ 4000 N] ; void insert(int u, int v, int w, int x){ edg[tot].fr = u, edg[tot].to = v, edg[tot].cap = w, edg[tot].flow = 0, edg[tot].cost = x; nxt[tot] = hd[u]; hd[u] = tot; tot++; edg[tot].fr = v, edg[tot].to = u, edg[tot].cap = 0, edg[tot].flow = 0, edg[tot].cost = -x; nxt[tot] = hd[v]; hd[v] = tot; tot++; } bool spfa1(int &fl, int &cst){ for(int i = s; i <= t; i++) d[i] = inf; d[s] = 0; p[s] = 0; a[s] = inf; int head = 0, tail = 1; q[0] = s; inq[s] = 1; while(head != tail){ int u = q[head++]; if(head == 1001) head = 0; inq[u] = 0; for(int i = hd[u]; i >= 0; i = nxt[i]){ Edge &e = edg[i]; if(d[e.to] > d[u] + e.cost && e.cap > e.flow){ d[e.to] = d[u] + e.cost; p[e.to] = i; a[e.to] = min(a[u], e.cap - e.flow); if(!inq[e.to]){ q[tail++] = e.to; if(tail == 1001) tail = 0; inq[e.to] = 1; } } } } if(d[t] == inf) return false; fl += a[t]; cst += a[t] * d[t]; int u = t; while(u != s){ edg[p[u]].flow += a[t]; edg[p[u]^1].flow -= a[t]; u = edg[p[u]].fr; } return true; } bool spfa2(int &fl, int &cst){ for(int i = s; i <= t; i++) d[i] = -inf; d[s] = 0; p[s] = 0; a[s] = inf; int head = 0, tail = 1; q[0] = s; inq[s] = 1; while(head != tail){ int u = q[head++]; if(head == 1001) head = 0; inq[u] = 0; for(int i = hd[u]; i >= 0; i = nxt[i]){ Edge &e = edg[i]; if(d[e.to] < d[u] + e.cost && e.cap > e.flow){ d[e.to] = d[u] + e.cost; p[e.to] = i; a[e.to] = min(a[u], e.cap - e.flow); if(!inq[e.to]){ q[tail++] = e.to; if(tail == 1001) tail = 0; inq[e.to] = 1; } } } } if(d[t] == -inf) return false; fl += a[t]; cst += a[t] * d[t]; int u = t; while(u != s){ edg[p[u]].flow += a[t]; edg[p[u]^1].flow -= a[t]; u = edg[p[u]].fr; } return true; } void init(){ scanf("%d", &n); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &mp[i][j]); } void build(){ memset(hd, -1, sizeof(hd)); tot = 0; s = 0, t = n * 2 + 1; for(int i = 1; i <= n; i++){ insert(s, i, 1, 0); insert(n + i, t, 1, 0); } for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) insert(i, n + j, inf, mp[i][j]); } void work(){ int flow = 0, cost = 0; build(); while(spfa1(flow, cost)); printf("%d\n", cost); flow = cost = 0; build(); while(spfa2(flow, cost)); printf("%d\n", cost); } int main(){ freopen("prog818.in", "r", stdin); freopen("prog818.out", "w", stdout); init(); work(); return 0; }
相关文章推荐
- 线性规划与网络流24题の18 分配问题(最小费用最大流、最大费用最大流)
- [网络流24题 #18]分配问题
- Codevs_P1915 分配问题(网络流24题最小费用流)
- 分配问题[网络流24题之18]
- 【网络流24题-18】分配问题(BSOI2558)
- [网络流24题] 18 分配问题(二分图最佳匹配,最小费用最大流)
- [网络流24题 #18]分配问题
- 网络流24题(07)试题库问题(二分图多重匹配 + 最大流)
- 网络流24题(09)方格取数问题(最大点权独立集 + 最小割最大流)
- 线性规划与网络流24题——04魔术球问题
- 网络流与线性规划24题10餐巾计划问题
- 网络流与线性规划24题09方格取数问题
- 网络流24题(11)航空路线问题(最大费用最大流)
- 网络流与线性规划24题02太空飞行计划问题
- 网络流与线性规划24题01飞行员配对方案问题
- 网络流24题(03)最小路径覆盖问题(二分图匹配 + 最大流)
- 网络流与线性规划24题11航空路线问题
- 线性规划与网络流24题——01飞行员配对方案问题
- 线性规划与网络流24题——04魔术球问题
- 网络流与线性规划24题04魔术球问题