poj 2987 Firing
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 10696 | Accepted: 3226 |
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
5 5
8
-9
-20
12
-10
1 2
2 5
1 4
3 4
4 5
Sample Output
2 2
Hint
最大权闭合图的求解方法是
先构造网络流N,添加源点s,从s到正权值点做一条边,容量为点的权值。
添加汇点t,从负权值点到t做一条边,容量为点的权值的绝对值。
原来的边的容量统统设为无穷大。
求解最小割,最大权=正权值之和-最小割权值
残余网络中的点的个数即为裁员个数。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
#include<queue>
#include<set>
#include<vector>
#include<cstring>
#include<string>
#define INF 0x3f3f3f3f3f3f3f3f
using namespace std;
const int N_MAX = +, M_MAX = , V_MAX = N_MAX + ; typedef long long ll;
int n, m;
struct edge {
int to; ll cap;int rev;
edge(int to, ll cap, int rev) :to(to), cap(cap), rev(rev) {}
};
vector<edge>G[V_MAX];
int level[V_MAX];
int iter[V_MAX]; void add_edge(int from, int to, ll cap) {
G[from].push_back(edge(to, cap, G[to].size()));
G[to].push_back(edge(from, , G[from].size() - ));
} void bfs(int s) {
memset(level, -, sizeof(level));
queue<int>que;
level[s] = ;
que.push(s);
while (!que.empty()) {
int v = que.front(); que.pop();
for (int i = ; i < G[v].size(); i++) {
edge&e = G[v][i];
if (e.cap > && level[e.to] < ) {
level[e.to] = level[v] + ;
que.push(e.to);
}
}
}
} ll dfs(int v, int t, ll f) {
if (v == t)return f;
for (int &i = iter[v]; i < G[v].size(); i++) {
edge&e = G[v][i];
if (e.cap > && level[v] < level[e.to]) {
ll d = dfs(e.to, t, min(f, e.cap));
if (d > ) {
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return ;
} ll max_flow(int s, int t) {
ll flow = ;
for (;;) {
bfs(s);
if (level[t] < )return flow;
memset(iter, , sizeof(iter));
ll f;
while ((f = dfs(s, t, INT_MAX))>) {
flow += f;
}
}
} int res = ;
bool vis[V_MAX];
void solve(int v) {//深搜找点
res++;
vis[v] = true;
for (int i = ; i < G[v].size();i++) {
edge e = G[v][i];
if (e.cap > && !vis[e.to]) {
solve(e.to);
}
}
} int main() {
while (scanf("%d%d",&n,&m)!=EOF) {
res = ; memset(vis, , sizeof(vis));
int s = , t = n+,V=t+;
ll sum = ;
for (int i = ; i < n;i++) {
ll a;
scanf("%lld",&a);
if (a > ) { add_edge(s, i + , a); sum += a; }
else add_edge(i + , t, -a);
}
for (int i = ; i < m;i++) {
int a, b;
scanf("%d%d",&a,&b);
add_edge(a,b,INF);
}
ll flow=max_flow(s, t);
solve(s);
printf("%d %lld\n",--res,sum-flow);
for (int i = ; i < V;i++) {//向量清空
G[i].clear();
}
}
return ;
}
poj 2987 Firing的更多相关文章
- poj 2987 Firing 最大权闭合图
题目链接:http://poj.org/problem?id=2987 You’ve finally got mad at “the world’s most stupid” employees of ...
- POJ 2987 - Firing - [最大权闭合子图]
题目链接:http://poj.org/problem?id=2987 Time Limit: 5000MS Memory Limit: 131072K Description You’ve fina ...
- POJ 2987 Firing 网络流 最大权闭合图
http://poj.org/problem?id=2987 https://blog.csdn.net/u014686462/article/details/48533253 给一个闭合图,要求输出 ...
- POJ 2987 Firing(最大权闭合图)
[题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...
- POJ 2987 Firing 最大流 网络流 dinic 模板
https://www.cnblogs.com/137shoebills/p/9100790.html http://poj.org/problem?id=2987 之前写过这道题,码一个dinic的 ...
- POJ 2987 Firing (最大权闭合图)
Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12108 Accepted: 3666 Descript ...
- POJ 2987 Firing(最大流最小割の最大权闭合图)
Description You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do ...
- POJ 2987 Firing【最大权闭合图-最小割】
题意:给出一个有向图,选择一个点,则要选择它的可以到达的所有节点.选择每个点有各自的利益或损失.求最大化的利益,以及此时选择人数的最小值. 算法:构造源点s汇点t,从s到每个正数点建边,容量为利益.每 ...
- POJ 2987 Firing | 最大权闭合团
一个点带权的图,有一些指向关系,删掉一个点他指向的点也不能留下,问子图最大权值 题解: 这是最大权闭合团问题 闭合团:集合内所有点出边指向的点都在集合内 构图方法 1.S到权值为正的点,容量为权值 2 ...
随机推荐
- navicate连接mysql
1. 打开navicate,选择连接 2. 编辑连接属性 编辑完成之后,连接成功.
- idea快速生成实体类Entity
1)打开idea 2)添加mysql的数据连接 3)生成类
- 分享一个Delphi跨平台Http库的封装,一个Delphi跨平台TCP库的封装
{ 单元名:跨平台的TCP客户端库封装 作者:5bug 网站:http://www.5bug.wang } unit uCPTcpClient; interface uses System.Class ...
- _IO_FILE
hctf2017的babyprintf解法是house of orange,深入学习了一下,牵扯出许多知识,这里先进行第一步:_IO_FILE结构 0x00 _IO_FILE glibc-2.2.1\ ...
- PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20)
PAT (Basic Level) Practise (中文)- 1024. 科学计数法 (20) http://www.patest.cn/contests/pat-b-practise/1024 ...
- linux uptime-查看Linux系统负载信息
更多linux 性能监测与优化 关注:linux命令大全 uptime命令能够打印系统总共运行了多长时间和系统的平均负载.uptime命令可以显示的信息显示依次为:现在时间.系统已经运行了多长时间.目 ...
- 关于$test$plusargs和$value$plusargs的小结
见: http://www.cnblogs.com/nanoty/p/4355245.html
- Python学习网站推荐
B站是目前本人看到的最好的免费学习Python的网站 黑马程序员- https://space.bilibili.com/37974444?spm_id_from=333.338.viewbox_re ...
- hdu 5878
I Count Two Three Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- JavaScript正则表达式-断言
(?=reg_pattern):正前向断言 只有当字符串右侧出现匹配reg_pattern的字符时才匹配正则表达式. str = "img1.jpg,img2.jpg,img3.bmp&qu ...