Firing

Time Limit: 5000MS Memory Limit: 131072K

Total Submissions: 11558 Accepted: 3494

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

As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.

Source

POJ Monthly–2006.08.27, frkstyc

首先我们要知道,这题要考察的是最大权闭合子图的姿势,不懂的OIEROIEROIER可以先看看这位大佬的博客

学习完了最大权闭合子图的知识过后,这道题做起来应该是比较轻松的了,我们可以参照求最大权闭合子图的方法,建立源点sss和汇点ttt,根据点权的正负性分别跟源点和汇点连边,在求出最小割之后dfsdfsdfs一遍sss所在的集合就可以得出最大权闭合子图了。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstdlib>
#define inf 0x3f3f3f3f
#define N 60000
#define M 3000000
using namespace std;
inline long long read(){
	long long ans=0,w=1;
	char ch=getchar();
	while(!isdigit(ch)){
		if(ch=='-')w=-1;
		ch=getchar();
	}
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+ch-'0',ch=getchar();
	return ans*w;
}
struct Node{long long v,next,c;}e[M<<1];
long long d[N],first[N],n,m,s,t,cnt=-1,ans=0,tot=0;
bool vis[N];
inline void add(long long u,long long v,long long c){
	e[++cnt].v=v;
	e[cnt].next=first[u];
	e[cnt].c=c;
	first[u]=cnt;
	e[++cnt].v=u;
	e[cnt].next=first[v];
	e[cnt].c=0;
	first[v]=cnt;
}
inline bool bfs(){
	queue<long long>q;
	q.push(s);
	memset(d,-1,sizeof(d));
	d[s]=0;
	while(!q.empty()){
		long long x=q.front();
		q.pop();
		for(long long i=first[x];i!=-1;i=e[i].next){
			long long v=e[i].v;
			if(d[v]!=-1||e[i].c<=0)continue;
			d[v]=d[x]+1;
			if(v==t)return true;
			q.push(v);
		}
	}
	return false;
}
inline long long dfs(long long x,long long f){
	if(x==t||!f)return f;
	long long flow=f;
	for(long long i=first[x];i!=-1;i=e[i].next){
		long long v=e[i].v;
		if(d[v]==d[x]+1&&flow&&e[i].c>0){
			long long tmp=dfs(v,min(e[i].c,flow));
			if(!tmp)d[v]=-1;
			flow-=tmp;
			e[i].c-=tmp;
			e[i^1].c+=tmp;
		}
	}
	return f-flow;
}
inline void dfs1(long long p){
	vis[p]=true;
	++tot;
	for(long long i=first[p];i!=-1;i=e[i].next){
		long long v=e[i].v;
		if(e[i].c>0&&!vis[v])dfs1(v);
	}
}
int main(){
	memset(first,-1,sizeof(first));
	memset(vis,false,sizeof(vis));
	n=read(),m=read(),s=0,t=n+1;
	for(long long i=1;i<=n;++i){
		long long x=read();
		if(x>0){
			add(s,i,x);
			ans+=x;
		}
		else add(i,t,-x);
	}
	for(long long i=1;i<=m;++i){
		long long u=read(),v=read();
		add(u,v,inf);
	}
	while(bfs())ans-=dfs(s,inf);
	dfs1(s);
	printf("%lld %lld",tot-1,ans);
	return 0;
}

2018.06.27Firing(最大权闭合子图)的更多相关文章

  1. 2018.11.06 NOIP训练 最大获利(profit)(01分数规划+最大权闭合子图)

    传送门 好题啊. ∑i<jpi,jK∗(200−K)>X\frac{\sum_{i<j}p_{i,j}}{K*(200-K)}>XK∗(200−K)∑i<j​pi,j​​ ...

  2. BZOJ1565 [NOI2009]植物大战僵尸(拓扑排序 + 最大权闭合子图)

    题目 Source http://www.lydsy.com/JudgeOnline/problem.php?id=1565 Description Input Output 仅包含一个整数,表示可以 ...

  3. HDU 3879 Base Station(最大权闭合子图)

    经典例题,好像说可以转化成maxflow(n,n+m),暂时只可以勉强理解maxflow(n+m,n+m)的做法. 题意:输入n个点,m条边的无向图.点权为负,边权为正,点权为代价,边权为获益,输出最 ...

  4. [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...

  5. HDU4971 A simple brute force problem.(强连通分量缩点 + 最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4971 Description There's a company with several ...

  6. HDU5855 Less Time, More profit(最大权闭合子图)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5855 Description The city planners plan to build ...

  7. HDU5772 String problem(最大权闭合子图)

    题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...

  8. SCU3109 Space flight(最大权闭合子图)

    嗯,裸的最大权闭合子图. #include<cstdio> #include<cstring> #include<queue> #include<algori ...

  9. hiho 第119周 最大权闭合子图

    描述 周末,小Hi和小Ho所在的班级决定举行一些班级建设活动. 根据周内的调查结果,小Hi和小Ho一共列出了N项不同的活动(编号1..N),第i项活动能够产生a[i]的活跃值. 班级一共有M名学生(编 ...

随机推荐

  1. Jmeter 录制脚本(一)

    第一种方法:使用Badboy来录制脚本 1. 启动Badboy, 工具栏上的红色圆形按钮是默认启动的,在地址栏直接输入被测试WEB项目的地址,然后点击右边的箭头. 2.录制完成后,点击工具栏上的黑色按 ...

  2. [LeetCode_105]Construct Binary Tree from Preorder and Inorder Traversal

    题目链接 https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意 由二叉 ...

  3. jquey中json字符串与json的转换(转)

    <!doctype html> <html> <head> <meta charset="utf-8"> <script sr ...

  4. 记录下ABAP开发的一些东西(T-code居多)Updated to markdown

    几个TCODE se38 开发program,report: sa38 只运行program se37 开发function: se11/se16 管理数据字典/数据表: ko03 Internal ...

  5. mysql8 公用表表达式CTE的使用

    公用表表达式CTE就是命名的临时结果集,作用范围是当前语句. 说白点你可以理解成一个可以复用的子查询,当然跟子查询还是有点区别的,CTE可以引用其他CTE,但子查询不能引用其他子查询. 一.cte的语 ...

  6. [z]表空间对应文件的AUTOEXTEND ON NEXT指定的值对性能的影响

    创建表空间的时候指定的数据文件可以设为自动扩展,以及每次扩展多少容量,如果发现在大数据量插入的时候非常慢,可能的原因是NEXT指定的值太小.下面来模拟一下这个过程:1,创建一个表空间:CREATE T ...

  7. vue缓存之keep-alive,设置想要缓存的页面

    由于项目需求从a页面跳转到b页面,返回a页面,a页面数据不能被刷新掉,方法很多列举12 方法1 a页面通过学期按钮切换学期,该学期里more进入b页面,返回a页面,返回回到对应a页面进入的高亮按钮设置 ...

  8. Python使用filetype精确判断文件类型 (文件类型获取)

    filetype.py Small and dependency free Python package to infer file type and MIME type checking the m ...

  9. Ubuntu安装时没注册root用户密码,怎么登录root

    一.其实我个人认为这没有多大必要,因为当你需要 root 的权限时,使用 sudo 便可以了.如果你实在需要在 Ubuntu 中启用 root 帐号的话,那么不妨执行下面的操作: 1.重新设置 roo ...

  10. c++ 备忘

    一.类型转换#include <sstream>stringstream ss;ss<<reverse(s1)<<'\t'<<reverse(s2);s ...