POJ 2987 Firing (最大权闭合图)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 12108 | Accepted: 3666 |
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
思路:
有一张图够了,来自:
https://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html
值得一提的是,我在DFS找点数的过程中,在残余网络中寻找时,限制了k(边下标)为偶数,以表示该边是原图中的边。(因为我的网络流奇数边是原图的反向边),但是WA,去掉这个限制就对了,目前不知道问题出在哪里。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const ll Inf = ;
const int mod = ;
//const double eps = 1e-6;
//const double pi = acos(-1);
int n,m,s,t;
int Head[],v[maxn],Next[maxn],cnt;
ll w[maxn];
void init(){
s=;t=n+;
memset(Head,-,sizeof(Head));
cnt=;
}
int vis[],num[];
void add(int x,int y,ll z)
{
// cout<<x<<" "<<y<<" "<<z<<endl;
if(x==y){return;}
v[cnt]=y;
w[cnt]=z;
Next[cnt]=Head[x];
Head[x]=cnt++; v[cnt]=x;
w[cnt]=;
Next[cnt]=Head[y];
Head[y]=cnt++;
} bool bfs()
{
memset(vis,,sizeof(vis));
for(int i=;i<=t;i++){
num[i]=Head[i];
}
vis[s]=;
queue<int>q;
q.push(s);
int r=;
while(!q.empty()){
int u=q.front();
q.pop();
int k=Head[u];
while(k!=-){
if(!vis[v[k]]&&w[k]){
vis[v[k]]=vis[u]+;
q.push(v[k]);
}
k=Next[k];
}
}
return vis[t];
} ll dfs(int u,ll f)
{ if(u==t){return f;}
int &k=num[u];
while(k!=-){
if(vis[v[k]]==vis[u]+&&w[k]){
ll d=dfs(v[k],min(f,w[k]));
if(d>){
w[k]-=d;
w[k^]+=d;
// fuck(d)
return d;
}
}
k=Next[k];
}
return 0ll;
}
ll Dinic()
{
ll ans=;
while(bfs()){
ll f;
while((f=dfs(s,Inf))>){
ans+=f;
}
}
return ans;
} int ans2=; void dfst(int x)
{
ans2++;
vis[x]=;
for(int k=Head[x];k!=-;k=Next[k]){
if(w[k]&&!vis[v[k]]){dfst(v[k]);}
}
} int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); scanf("%d%d",&n,&m);
init();
ll all=;
for(int i=;i<=n;i++){
ll x;
scanf("%lld",&x);
if(x>){all+=x;add(s,i,x);}
else{
add(i,t,-x);
}
}
for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
add(x,y,Inf);
}
ll ans1=all-Dinic(); memset(vis,,sizeof(vis)); dfst(s);
printf("%d %lld\n",ans2-,ans1);
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 | 最大权闭合团
一个点带权的图,有一些指向关系,删掉一个点他指向的点也不能留下,问子图最大权值 题解: 这是最大权闭合团问题 闭合团:集合内所有点出边指向的点都在集合内 构图方法 1.S到权值为正的点,容量为权值 2 ...
- POJ2987 Firing 最大权闭合图
详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...
- POJ 2987:Firing(最大权闭合图)
http://poj.org/problem?id=2987 题意:有公司要裁员,每裁一个人可以得到收益(有正有负),而且如果裁掉的这个人有党羽的话,必须将这个人的所有党羽都裁除,问最少的裁员人数是多 ...
- 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(最大流最小割の最大权闭合图)
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(最大权闭合图+割边最少)
题目链接:http://poj.org/problem?id=2987 思路:标准的最大权闭合图,构图:从源点s向每个正收益点连边,容量为收益:从每个负收益点向汇点t连边,容量为收益的相反数:对于i是 ...
随机推荐
- APP test
在讲APP测试之前,先讲一下,目前APP的操作系统以及APP相关基础知识. 一.APP基础知识 1.操作系统# 现在移动端的操作系统主流的分为两种:(1)安卓系统 (2)IOS系统. 2.安卓系统# ...
- Ubuntu16.04网络不能访问解决办法
问题: 系统重启后,网络不能正常使用,加载网络配置失败,且重启网络时也提示错误. 解决方法: 在定位的过程中发现是配置中的网络设备号与实际设备号不符. 1.查看网络配置中的配备号: vi /etc ...
- PDO访问Mysql数据库
$dsn = 'mysql:host=127.0.0.1;dbname=myblog'; $username = 'root'; $pwd = '; $pdo = new PDO($dsn,$user ...
- LODOP用ADD_PRINT_IMAGE语句缩放打印图片
LODOP提高输出图片质量的方法:1.用ADD_PRINT_IMAGE语句打印图片,而且img元素的width和height属性要去掉或者设置足够大,这样就可以让下载引擎传给Lodop图片质量足够好; ...
- js函数使用prototype和不适用prototype的区别
js中类定义函数时用prototype与不用的区别 原创 2017年06月05日 12:25:41 标签: 函数 / prototype / class 首先来看一个实例: function Li ...
- Spring Boot 构建电商基础秒杀项目 (十一) 秒杀
SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...
- c++ string去除首尾 空格、\n、\r、\t
string s = " test "; size_t n = s.find_last_not_of(" \r\n\t"); if (n != string:: ...
- iOS的非常全的三方库,插件,大牛博客
转自: http://www.cnblogs.com/zyjzyj/p/6015625.html github排名:https://github.com/trending, github搜索:http ...
- h.264并行熵解码
在前面讨论并行解码的章节中,我们专注于讨论解码的宏块重建部分,甚至把宏块重建描述成宏块解码,这是因为在解码工作中,宏块重建确实占了相当大的比重,不过解码还包含其它的部分,按照解码流程可粗略分为: 读取 ...
- Nginx 优化缓冲区与传输效率
L:126 这里简单的做个计算 比如 我的服务器带宽是 5M=41943040字节 如果按照公网用PIND的得到延迟结果 icmp_seq=3 ttl=49 time=35.612 ms BDP = ...