Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 10905   Accepted: 3291

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 ≤ in). The remaining m lines each contain two integers i and j (1 ≤ i, jn) 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

【问题描述】 
在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,
如果该下属还有下属,照斩不误。给出每个人的贡献值和从属关系,求在最大贡献值的前提下最小剩下多少人及最大贡献值。留下多少人无所谓,现在老板想知道留下的人最大的贡献值是多少。
【输入描述】
    第一行两个整数n,m,表示有多少个员工与多少个从属关系。
第二行n个整数,表示每个员工的贡献值。
接着m行,每行两个数x,y,表示y是x的下属,一个员工可能有多个下属但不会有多个上司
【输出描述】
包括两个数,表示最大贡献值前提下最小剩下多少人及最大贡献值和。
【样例输入】:
5 5
8
-9
-20
12
-10
1 2
2 5
1 4
3 4
4 5
【样例输出】:
2 2
【其他说明】:
0 < n ≤ 5000
0 ≤ m ≤ 60000
员工价值≤107
样例中留下4,5号员工是最好情况 基础知识点这里
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=;
const long long INF=0x3f3f3f3f3f3f;
int head[N],tot,S,T;
int q[N],dis[N],n,m;
bool vis[N];
struct node
{
int next,v;
long long w;
} e[M<<];
void add(int u,int v,long long w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
bool bfs()
{
memset(dis,-,sizeof(dis));
dis[S]=;
int l=,r=;
q[r++]=S;
while(l<r)
{
int u=q[l++];
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].v;
if(dis[v]==-&&e[i].w>)
{
q[r++]=v;
dis[v]=dis[u]+;
if(v==T) return true;
}
}
}
return false;
}
long long dfs(int s,long long low)
{
if(s==T||!low) return low;
long long ans=low,a;
for(int i=head[s]; ~i; i=e[i].next)
{
if(e[i].w>&&dis[e[i].v]==dis[s]+&&(a=dfs(e[i].v,min(e[i].w,ans))))
{
e[i].w-=a;
e[i^].w+=a;
ans-=a;
if(!ans) return low;
}
}
if(low==ans) dis[s]=-;
return low-ans;
}
void dfs2(int &cnt,int u){
for(int i=head[u];~i;i=e[i].next){
if(!vis[e[i].v]&&e[i].w>) {
vis[e[i].v]=;
++cnt;
dfs2(cnt,e[i].v);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
S=,T=n+;
int x,y;
long long ans=;
long long c;
memset(head,-,sizeof(head));
tot=;
for(int i=; i<=n; ++i)
{
scanf("%I64d",&c);
if(c>=) {add(S,i,c),add(i,S,);ans+=c;}
else add(i,T,-c),add(T,i,);
}
while(m--){
scanf("%d%d",&x,&y);
add(x,y,INF);
add(y,x,);
}
while(bfs()) ans-=dfs(S,INF);
memset(vis,,sizeof(vis));
vis[S]=;
int cnt=;
dfs2(cnt,);
printf("%d %I64d\n",cnt,ans);
}
}

最新文章

  1. sql server中对xml进行操作
  2. 第三个Sprint团队贡献分
  3. 在线调试js工具网站
  4. uploadify 下载组件使用技巧和在线预览 word,excel,ppt,pdf的方案
  5. Eclipse设置、问题解决方案
  6. HTTP协议-标签简介
  7. Zab: A simple totally ordered broadcast protocol(译)
  8. JSON解析的几种方式
  9. [SDOI2016]硬币游戏
  10. jdbc 增删改查以及遇见的 数据库报错Can&#39;t get hostname for your address如何解决
  11. nginx报错 [error] open() “/usr/local/var/run/openresty.pid” failed (2: No such file or directory)
  12. ORM关于表那些事
  13. Linux文件权限设置
  14. DataGrid列中加入CheckBox 全选 点击Header全选 和 只操作选中部分 功能的实现
  15. SwitchButton 开关按钮 的多种实现方式 (附源码DEMO)
  16. Swift 类型嵌套
  17. 前端框架之Vue(5)-条件渲染
  18. java读取按行txt文件
  19. 解决svchost占用内存过高问题
  20. selenium webdriver——JS滚动到最底部

热门文章

  1. 初篇:我与Linux
  2. Algs4-2.1.17动画-选择排序
  3. 算法竞赛进阶指南--hamilton路径
  4. 一只简单的网络爬虫(基于linux C/C++)————浅谈并发(IO复用)模型
  5. Web框架,Hibernate向数据库插入数据,数据库没有值怎么办?
  6. jQuery中bind()与on()绑定事件的区别
  7. hue搭建
  8. mysql中的读锁和写锁
  9. 036_python的大文件下载以及进度条展示
  10. LeetCode--Array--Remove Element &amp;&amp; Search Insert Position(Easy)