poj 2987 Firing 最大权闭合子图
2015-08-03 16:47
489 查看
Firing
Description
You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do some firings. You’re now simply too mad to give response to questions like “Don’t you think it is an even more stupid decision to have signed them?”, yet calm enough to consider the potential profit and loss from firing a good portion of them. While getting rid of an employee will save your wage and bonus expenditure on him, termination of a contract before expiration costs you funds for compensation. If you fire an employee, you also fire all his underlings and the underlings of his underlings and those underlings’ underlings’ underlings… An employee may serve in several departments and his (direct or indirect) underlings in one department may be his boss in another department. Is your firing plan ready now?
Input
The input starts with two integers n (0 < n ≤ 5000) and m (0 ≤ m ≤ 60000) on the same line. Next follows n + m lines. The first n lines of these give the net profit/loss from firing the i-th employee individually bi (|bi| ≤ 107, 1 ≤ i ≤ n). The remaining m lines each contain two integers i and j (1 ≤ i, j ≤ n) meaning the i-th employee has the j-th employee as his direct underling.
Output
Output two integers separated by a single space: the minimum number of employees to fire to achieve the maximum profit, and the maximum profit.
Sample Input
Sample Output
Hint
As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.
链接:http://poj.org/problem?id=2987
最大权闭合图裸题。
最大权闭合图 参考 胡伯涛 算法合集之《最小割模型在信息学竞赛中的应用》
闭合图(closure):是有向图点一个点集,且
该点集的所有出边都还指向该点集。即闭合图点任意点点任意后继也一定在闭合图中。
最大权闭合图(maximum weight closure):是一个点权之和最大点闭合图。
闭合图点性质恰好反映来事件间点必要条件的关系。
闭合图问题建图方法:
在原图点集的基础上增加源s和汇t,对于原图中点每条边(u,v),在图中连接u,v点,容量为正无穷。
对于权值为正的点。连接源s和该点,容量为该点的权值。
对于权值为负点点。连接汇t和该点。容量为该点的权值。
最大权值 = 所有正权值之和 - 最小割
hdu 3061 Battle http://acm.hdu.edu.cn/showproblem.php?pid=3061 和这题一样
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 8952 | Accepted: 2690 |
You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do some firings. You’re now simply too mad to give response to questions like “Don’t you think it is an even more stupid decision to have signed them?”, yet calm enough to consider the potential profit and loss from firing a good portion of them. While getting rid of an employee will save your wage and bonus expenditure on him, termination of a contract before expiration costs you funds for compensation. If you fire an employee, you also fire all his underlings and the underlings of his underlings and those underlings’ underlings’ underlings… An employee may serve in several departments and his (direct or indirect) underlings in one department may be his boss in another department. Is your firing plan ready now?
Input
The input starts with two integers n (0 < n ≤ 5000) and m (0 ≤ m ≤ 60000) on the same line. Next follows n + m lines. The first n lines of these give the net profit/loss from firing the i-th employee individually bi (|bi| ≤ 107, 1 ≤ i ≤ n). The remaining m lines each contain two integers i and j (1 ≤ i, j ≤ n) meaning the i-th employee has the j-th employee as his direct underling.
Output
Output two integers separated by a single space: the minimum number of employees to fire to achieve the maximum profit, and the maximum profit.
Sample Input
5 5 8 -9 -20 12 -10 1 2 2 5 1 4 3 4 4 5
Sample Output
2 2
Hint
As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.
链接:http://poj.org/problem?id=2987
最大权闭合图裸题。
最大权闭合图 参考 胡伯涛 算法合集之《最小割模型在信息学竞赛中的应用》
闭合图(closure):是有向图点一个点集,且
该点集的所有出边都还指向该点集。即闭合图点任意点点任意后继也一定在闭合图中。
最大权闭合图(maximum weight closure):是一个点权之和最大点闭合图。
闭合图点性质恰好反映来事件间点必要条件的关系。
闭合图问题建图方法:
在原图点集的基础上增加源s和汇t,对于原图中点每条边(u,v),在图中连接u,v点,容量为正无穷。
对于权值为正的点。连接源s和该点,容量为该点的权值。
对于权值为负点点。连接汇t和该点。容量为该点的权值。
最大权值 = 所有正权值之和 - 最小割
hdu 3061 Battle http://acm.hdu.edu.cn/showproblem.php?pid=3061 和这题一样
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <string> #include <queue> #include <vector> using namespace std; #define maxn 5010 const int inf = 0x3f3f3f3f; struct Edge { int from, to, cap, flow; Edge(int f, int t, int c, int fl) { from = f; to = t; cap = c; flow = fl; } }; vector <Edge> edges; vector <int> G[maxn]; int vis[maxn], d[maxn], cur[maxn]; int s, t, n, m; long long min(long long a, long long b) { return a<b?a:b; } void AddEdge(int from, int to, int cap) { edges.push_back(Edge(from, to, cap, 0)); edges.push_back(Edge(to, from, 0, 0)); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool bfs() { memset(vis, 0, sizeof(vis)); d[s] = 0; vis[s] = 1; queue <int> q; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = 0; i < G[u].size(); i++) { Edge &e = edges[G[u][i]]; if(!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[u]+1; q.push(e.to); } } } return vis[t]; } long long dfs(int x, long long a) { if(x == t || a == 0) return a; long long flow = 0, f; for(int &i = cur[x]; i < G[x].size(); i++) { Edge &e = edges[G[x][i]]; if(d[x]+1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) { e.flow += f; edges[G[x][i]^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } long long Maxflow() { long long flow = 0; while(bfs()) { memset(cur, 0, sizeof(cur)); flow += dfs(s, inf); } return flow; } int N, M; int main() { while(~scanf("%d%d", &N, &M)) { edges.clear(); for(int i = 0; i <= N+1; i++) G[i].clear(); s = 0; t = N+1; long long sum = 0; for(int i = 1; i <= N; i++) { long long temp; scanf("%lld", &temp); if(temp > 0) {AddEdge(s, i, temp); sum += temp;} else if(temp < 0) AddEdge(i, t, -temp); } for(int i = 1; i <= M; i++) { int a, b; scanf("%d%d", &a, &b); AddEdge(a, b, inf); } sum -= Maxflow(); int ans = 0; for(int i = 1; i <= N; i++) { if(vis[i]) ans++; } printf("%d %lld\n", ans, sum); } return 0; }
相关文章推荐
- Win10C:\Windows\System32\drivers\etc\hosts提示权限不够问题
- 升级Win10时出现错误代码80240020的解决办法
- link
- 在linux下利用nohup来后台运行java程序
- CoreData使用教程
- 判断view 是否显示在scrollview 上
- 2.3-5 BINARY-SEARCH
- const 和 #define 的区别
- iOS开发UI篇—UIScrollView控件实现图片轮播
- Android ANR-Thermal condition
- exfat文件系统(五)------文件read和write
- 使用Studio两周了
- linux服务器架构设计 金融行业CMS系统)第三篇 【架构拓扑图-软件准备】
- 重学java23种设计模式(10)外观模式
- java poi导出execl类
- 泊松图像编辑(Poisson image editing)
- Android屏幕保护KeyguardManager相关内容学习
- FZU 1650 1752 a^b mod c
- Web项目中 .classpath、.mymetadata、.project文件的作用详解
- php ajax 实时显示时间