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 ...
随机推荐
- Adding other views to UIButton
Q: I want to add some views to UIButton, for example multiple UILabels, UIImages etc. One I add thos ...
- The Django Book - 第四章 模板2
模板(相应)使用的几种方式: 1.使用HttpResponse返回字符串HTML from django.http import HttpResponse def current_datetime(r ...
- 通过90行代码学会HTML5 WebSQL的4种基本操作
Web SQL数据库API是一个独立的规范,在浏览器层面提供了本地对结构化数据的存储,已经被很多现代浏览器支持了. 我们通过一个简单的例子来了解下如何使用Web SQL API在浏览器端创建数据库表并 ...
- (转)linux自动备份oracle数据库并上传到备份服务器 脚本实现
实际项目中,备份数据是不可缺少的一步,完成数据的自动备份减少个人的工作量,是我们的目标.之前很少写过脚本,不过这些简单的操作还是可以做到的!话不多说,开始具体介绍:oracle版本:10.2.0操作系 ...
- Dance links算法
其实Dance links只是一种数据结构,Dance links 才是一种算法.dacing links x就是一个高效的求解该类问题的算法,而这种算法,基于交叉十字循环双向 链表.下面是双向十字链 ...
- CAD控件界面显示与隐藏(网页版)
控件界面工具栏的显示或隐藏,js代码实现如下: 1 2 3 4 5 6 7 8 9 //隐藏/显示工具栏 mxOcx.ShowToolBar("常用工具",isShow ...
- C++ static关键字
一.面向过程中的static 1.修饰全局变量(静态全局变量) (1)静态全局变量在全局数据区分配内存: (2)未经初始化的静态全局变量会被程序自动初始化为0: (3)静态全局变量在申明它的整个文件是 ...
- javase(4)_数组
一.数组概述 数组可以看成是多个相同类型数据组合,对这些数据的统一管理. 数组变量属于引用类型,数组也可以看成对象,数组中的每个元素相当于该对象的成员变量. 数组中的元素可以是任意类型,包括基本类型和 ...
- 【树状数组 思维题】luoguP3616 富金森林公园
树状数组.差分.前缀和.离散化 题目描述 博艾的富金森林公园里有一个长长的富金山脉,山脉是由一块块巨石并列构成的,编号从1到N.每一个巨石有一个海拔高度.而这个山脉又在一个盆地中,盆地里可能会积水,积 ...
- 使用Hexo+Github搭建属于自己的博客
工具:Visual Studio Code/MarkdownPad技术:Hexo+Github 创建Github项目 Github账户注册和新建项目,项目必须要遵守格式:账户名.github.io,不 ...