40th Asia Region, Tehran Site, Problem I: Cafebazaar【最大费用可行流】
2016-08-05 19:47
225 查看
题目大意
就是有n个人,m个app,有几个人是关键人,必须得开发app,有几个app是关键app,必须得被开发。然后每个人仅可以开发指定的几个app,分别可获得不同的钱
每个人仅可开发一个app,一个app仅可有一个人开发。
问你,在关键人和关键app能不能满足,能的话,最大钱是多少。
思路
以样例为例:2 4 1 1 1 3 2 1 8 2 10 3 2 2 3 10 4 50
建立网络流如上,所有边的容量都为1,关键点的权值置为INF,中间的边权值为开发app的钱,然后跑一次最大费用可行流。
最大费用可行流,在最小费用最大流的模板上稍加修改就能实现。
先说最小费用流
算法步骤
初始化伴随网络:
<v,u>.cost=-<u,v>.cost,<v,u>.cap=flow=0
即对原网络中每一条边,添加一条容量为0的反向边,费用取相反数,表示这正向边少流可以减少费用。
然后每次在伴随网络里用SPFA求费用最短的增广路
一开始 反向负费用的边 ,容量都是0
所以第一次跑出来的最短路,费用一定是正的
然后沿着这条增广路增广一次, 增广途中就有反向边的容量被增加了
然后第二次跑最短路,就有可能跑出负费用
如果是负费用,说明费用还可以优化
但如果再次跑出了正费用,说明费用已经没法优化了,但流还可以继续增广
这时候继续进行到底,就是最小费用最大流;停住,就是最小费用可行流
最大费用只需把边的费用取相反数,最后答案也取相反数即可。
代码有点丑将就着看看吧- -。
AC代码
/* 40th 2015-2016 Asia Region, Tehran Site Problem I: Cafebazaar */ #include <iostream> #include <iomanip> #include <fstream> #include <sstream> #include <cmath> #include <cstdio> #include <cstring> #include <cctype> #include <algorithm> #include <functional> #include <numeric> #include <string> #include <set> #include <map> #include <stack> #include <vector> #include <queue> #include <deque> #include <list> //#include <unordered_map> using namespace std; #define CLR(x,y) memset((x),(y),sizeof(x)) typedef long long ll; typedef unsigned long long ull; //typedef __int128 lll; const int MAXN = 10000; const int MAXM = 100000; const ll INF = 0x3f3f3f3f; struct Edge { int to, next, cap, flow, cost; }edge[MAXM]; int head[MAXN], tol; int pre[MAXN]; ll dis[MAXN]; bool vis[MAXN]; int N;//节点总个数 void init(int n) { N = n; tol = 0; CLR(head,-1); } void addedge(int u, int v, int cap, int cost) { edge[tol].to = v; edge[tol].cap = cap; edge[tol].cost = -cost;//最大费用 edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++; edge[tol].to = u; edge[tol].cap = 0; edge[tol].cost = cost; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++; } bool spfa(int s, int t) { queue<int>q; for (int i = 0; i <= N; i++) { dis[i] = INF; vis[i] = false; pre[i] = -1; } dis[s] = 0; vis[s] = true; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; pre[v] = i; if (!vis[v]) { vis[v] = true; q.push(v); } } } } if (pre[t] == -1)return false; else return true; } //cost存的是最大费用 int maxCostflow(int s, int t, ll &cost) { int flow = 0; cost = 0; bool first=0; while (spfa(s, t)) { int Min = INF; for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) { if (Min > edge[i].cap - edge[i].flow) Min = edge[i].cap - edge[i].flow; } ll c=0; for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to]) { edge[i].flow += Min; edge[i ^ 1].flow -= Min; c += edge[i].cost * Min; } //printf("cost:%d\n",c); if(first)first=0; else if(c>=0)return flow; cost+=c; flow += Min; } return flow; } bool has_id[300]; int main() { int n,m; while(scanf("%d%d",&n,&m),n) { CLR(has_id,0); init(n+m+1); int t,cntINF=0; scanf("%d",&t); cntINF+=t; while(t--) { int x; scanf("%d",&x); addedge(0,x,1,INF); has_id[x]=1; } for(int i=1 ; i<=n ; ++i) { if(!has_id[i]) addedge(0,i,1,0); } scanf("%d",&t); cntINF+=t; while(t--) { int x; scanf("%d",&x); x+=n; addedge(x,n+m+1,1,INF); has_id[x]=1; } for(int i=n+1 ; i<=n+m ; ++i) { if(!has_id[i])addedge(i,n+m+1,1,0); } for(int i=1 ; i<=n ; ++i) { int t; scanf("%d",&t); while(t--) { int v,w; scanf("%d%d",&v,&w); v+=n; addedge(i,v,1,w); } } ll ans=0; maxCostflow(0,n+m+1,ans); ans=-ans; ans-=cntINF*INF; if(ans>=0)printf("%lld\n",ans); else printf("-1\n"); } return 0; }
相关文章推荐
- Regionals 2015 >> Asia - Tehran >> 7530 - Cafebazaar【二分图最佳匹配】【最小费用可行流】
- HDU 2426 Interesting Housing Problem【最大费用最大流 && 常规题】
- hdoj--2426--Interesting Housing Problem(最大费用流)
- HDU 6118 度度熊的交易计划 最大费用可行流
- http://acm.nyist.net/JudgeOnline/problem.php?pid=61&&最大费用最大流
- hdoj 2246 Interesting Housing Problem 【最大费用最大流 or KM算法】
- [对偶 KM算法 生成树 || 最大费用可行流 || 线性规划] BZOJ 1937 [Shoi2004]Mst 最小生成树
- [LP对偶 & 最大费用可行流] TopCoder SRM 676 div1 Farmville
- ACdream1171 下界转上界-最大费用可行流
- 【有上下界最大费用可行流】HDU-4862 Jump
- POJ 2426 Interesting Housing Problem (最大费用最大流)
- hdu 6118 最小费用可行流(注意与最大流的区别)
- POJ 2426 Interesting Housing Problem (最大费用最大流)
- [最大费用可行流 || 单纯形] BZOJ 3118 Orz the MST
- [无源汇最大费用可行流 差分费用流] Codeforces 717G Bubble Cup 9 - Finals G. Underfail
- HDU 4406 GPA 最大费用流
- poj 3422 Kaka's Matrix Travels(最大费用流+拆点)
- ZOJ 3904 Beer Problem 最大费用最大流
- HDU 5045 费用流求最大权
- bzoj 4514: [Sdoi2016]数字配对(二分图+费用最大流)