UVALive - 3487 Duopoly(最小割)
来源:互联网 发布:买网络教育本科多少钱 编辑:程序博客网 时间:2024/05/30 22:59
题目大意:有两个公司A和B在申请一些资源
现在给出两个公司所申请的内容,内容包括价钱和申请的资源
现在你做为官方,你只能拒绝一个申请或者接受一个申请,同一个资源不能两个公司都拥有,且申请的资源不能只给部分
问:作为官方,你能得到的最大利益是多少
解题思路:这题的矛盾在于,同一个资源不能两家公司共享,既然如此的话,那就找出矛盾的申请,让他们之间连条线就可以了,容量为INF
设立源点,源点和A公司的申请相连接,容量为申请的价钱
设立汇点,汇点和B公司的申请相连接,容量为申请的价钱
然后找出矛盾的申请,连线。因为只有矛盾的申请才能连线,且只有连线的才能把流流到汇点,所以跑出来的最大流就是我们所需要的最小割
所以答案就是所有申请的价钱总和 - 最小割
如果不懂的话,请百度最小割,去掉最小割后,两个点集之间就没有关系,也就不会存在矛盾了
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;#define M 1000010#define N 10010#define INF 0x3f3f3f3fstruct Edge{ int u, v, cap, flow, next;}E[M];struct Dinic{ int head[N], d[N]; int tot, sink, source; void init() { memset(head, -1, sizeof(head)); tot = 0; } inline void AddEdge(int u, int v, int cap) { E[tot].u = u; E[tot].v = v; E[tot].cap = cap; E[tot].flow = 0; E[tot].next = head[u]; head[u] = tot++; u = u ^ v; v = u ^ v; u = u ^ v; E[tot].u = u; E[tot].v = v; E[tot].cap = 0; E[tot].flow = 0; E[tot].next = head[u]; head[u] = tot++; } inline bool bfs(int s) { int u, v; memset(d, 0, sizeof(d)); queue<int> Q; Q.push(s); d[s] = 1; while (!Q.empty()) { u = Q.front(); Q.pop(); if (u == sink) return true; for (int i = head[u]; ~i; i = E[i].next) { v = E[i].v; if (!d[v] && E[i].cap - E[i].flow > 0) { d[v] = d[u] + 1; Q.push(v); } } } return false; } int dfs(int x, int a) { if (x == sink || a == 0) return a; int f, flow = 0; for (int i = head[x]; ~i; i = E[i].next) { int v = E[i].v; if (d[v] == d[x] + 1 && E[i].cap - E[i].flow > 0) { f = dfs(v, min(a, E[i].cap - E[i].flow)); E[i].flow += f; E[i^1].flow -= f; flow += f; a -= f; if (!a) break; } } if (flow == 0) d[x] = 0; return flow; } int Maxflow(int source, int sink) { int flow = 0; this->sink = sink; while (bfs(source)) flow += dfs(source, INF); return flow; }};Dinic dinic;#define maxn 3010#define S 300010int PriceTel[maxn], PriceMob[maxn];int ChanTel[S], ChanMob[S];bool vis[maxn][maxn];int n, m, cas = 1;void init() { memset(ChanTel, 0, sizeof(ChanTel)); memset(ChanMob, 0, sizeof(ChanMob)); memset(vis, 0, sizeof(vis)); int Max = -INF, ans = 0; char c; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &PriceTel[i]); ans += PriceTel[i]; c = getchar(); while (c != '\n') { int t = 0; while (c = getchar(), c >= '0' && c <= '9') t = t * 10 + c - '0'; Max = max(Max, t); ChanTel[t] = i; } } scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%d", &PriceMob[i]); ans += PriceMob[i]; c = getchar(); while (c != '\n') { int t = 0; while (c = getchar(), c >= '0' && c <= '9') t = t * 10 + c - '0'; Max = max(Max, t); ChanMob[t] = i; } } int source = 0, sink = n + m + 1; dinic.init(); for (int i = 1; i <= n; i++) dinic.AddEdge(source, i, PriceTel[i]); for (int i = 1; i <= m; i++) dinic.AddEdge(i + n, sink, PriceMob[i]); for (int i = 1; i <= Max; i++) { if (!ChanTel[i] || !ChanMob[i] || vis[ChanTel[i]][ChanMob[i]]) continue; vis[ChanTel[i]][ChanMob[i]] = true; dinic.AddEdge(ChanTel[i], ChanMob[i] + n, INF); } int maxflow = dinic.Maxflow(source, sink); printf("Case %d:\n", cas++); printf("%d\n", ans - maxflow);}int main() { int test; scanf("%d", &test); bool flag = false; while (test--) { if (flag) printf("\n"); flag = true; init(); } return 0;}
0 0
- UVALive - 3487 Duopoly(最小割)
- LA 3487 Duopoly 最小割
- UVA 1212 - Duopoly(最小割)
- UVA 1212 - Duopoly(最小割)
- Duopoly UVALive
- uva 1212 Duopoly (最小割最大流)
- UVALive 7264 (最小割)
- (beginer)网络流(最小割最大流) UVA 1212 - Duopoly
- UVALive 7264 Kejin Game(最小割)
- 【UVALive】3661 Animal Run 平面图最小割 最短路
- UVALive 7264 Kejin Game 2015北京现场赛【最小割】
- UVALive 7264 Kejin Game (最大流最小割)
- UVALIVE 3661 Animal Run <最大-最小原理之一 最大流-最小割原理>
- 【割点 && dfs】UVALive
- 最小割
- 最小割
- 最小割
- 最小割
- phalcon mysql 日志 添加
- UIAlertView(提示框)
- POI实战-java开发excel详解(第四章 常用操作-下拉列表)
- 浅析XSS攻击和防御
- Flume - FileChannel源码详解
- UVALive - 3487 Duopoly(最小割)
- UIActionSheet
- 概念:malloc/free与new/delete的区别
- linux常用命令总结(转自董的博客)
- UNIX网络编程卷一:调试工具 tcpdump lsof 总结
- UINavigationController操作
- java错误-The prefix "aop" for element "aop:aspectj-autoproxy" is not bound.
- POI实战-java开发excel详解(第五章 样式)
- 实现matlab直方图的网格填充