【题目】

传送门

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

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.

【分析】

大致题意:一个公司有  个员工  对从属关系,每辞退一个员工可以得到一个价值  可以为负),但每辞退一个员工都要把他的下属一起辞退,求能获得的最大价值时辞退的最少员工数以及能获得的最大价值

在说这道题之前,先说一下最大权闭合子图吧

不太规范的定义:一个子图(点集),如果所有点的所有出边都在这个子图当中,那么它就是闭合子图。点权和最大的闭合子图就是最大闭合子图。

求法:新增两个虚点,一个是源点 ,一个是汇点 ;设这个图中一个点的点权为 ,若  ≥ 0,则从  向这个点连一条边权为  的边,不然就从这个点向  连一条边权为  的边;图中原有的边的边权就是 ;跑完最大流后,原来正的点权和减去最大流就是答案

证明的话我不会……

然后看这道题,每个上司都向下属连边,然后删掉点  后要把从  连边的所有点删掉,根据定义,这就是一道典型的最大权闭合子图,连边跑一遍最大流即可

对于第一问,被辞退的员工应该是会被全部流满的,因此再  一遍即可

【代码】

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100005
#define M 10000005
#define inf (1ll<<31ll)-1
using namespace std;
int n,m,s,t,num=1;
int v[M],w[M],next[M];
int d[N],f[N],first[N];
bool vis[N];
void add(int x,int y,int k)
{num++;next[num]=first[x];first[x]=num;v[num]=y;w[num]=k;
}
bool bfs()
{int x,y,i,j;memset(d,-1,sizeof(d));memcpy(f,first,sizeof(f));queue<int>q;d[s]=0;q.push(s);while(!q.empty()){x=q.front();q.pop();for(i=first[x];i;i=next[i]){y=v[i];if(w[i]&&d[y]==-1){d[y]=d[x]+1;if(y==t)return true;q.push(y);}}}return false;
}
int dinic(int now,int flow)
{if(now==t)  return flow;int x,delta,ans=0;for(int &i=f[now];i;i=next[i]){x=v[i];if(w[i]&&d[x]==d[now]+1){delta=dinic(x,min(flow,w[i]));w[i]-=delta,w[i^1]+=delta;ans+=delta,flow-=delta;if(!flow)  return ans;}}return ans;
}
int dfs(int x)
{int i,j,ans=1;vis[x]=true;for(i=first[x];i;i=next[i]){j=v[i];if(w[i]&&!vis[j])ans+=dfs(j);}return ans;
}
int main()
{int x,y,i,k;long long sum=0;scanf("%d%d",&n,&m);s=0,t=n+1;for(i=1;i<=n;++i){scanf("%d",&k);if(k<0)  add(i,t,-k),add(t,i,0);else  add(s,i,k),add(i,s,0),sum+=k;}for(i=1;i<=m;++i){scanf("%d%d",&x,&y);add(x,y,inf),add(y,x,0);}while(bfs())sum-=dinic(s,inf);int ans=dfs(s)-1;printf("%d %lld",ans,sum);return 0;
}

【POJ 2987】Firing相关推荐

  1. 【POJ - 2987】Firing(最大权闭合图,网络流最小割,输出方案最小,放大权值法tricks)

    题干: You've finally got mad at "the world's most stupid" employees of yours and decided to ...

  2. 【POJ - 2987 】Firing 【最大权闭合图有唯一的 势 】

    You've finally got mad at "the world's most stupid" employees of yours and decided to do s ...

  3. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  4. BZOJ 2287 【POJ Challenge】消失之物

    2287: [POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了. &q ...

  5. 【POJ 3026】Borg Maze

    [POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队能够无限切割 问搜索到全部alien所须要的总步数 即求一个无向图 包括全部的点而且总权值最小(最小生成树 BFS+最小生成 ...

  6. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  7. 【POJ 2485】 Highways

    [POJ 2485] Highways 最小生成树模板 Prim #includeusing namespace std;int mp[501][501]; int dis[501]; bool vi ...

  8. 2287. 【POJ Challenge】消失之物(数组递推\分治优化背包)

    2287. [POJ Challenge]消失之物 这题的思想和P4564 [CTSC2018]假面优化的思想一样,应该反过来说,假面那个题应该是借鉴这题的思路. 显然不能枚举每个物品消失O(n)O( ...

  9. bzoj2287【POJ Challenge】消失之物 缺一01背包

    bzoj2287[POJ Challenge]消失之物 缺一01背包 链接 bzoj 思路 分治solve(l,r,arr)表示缺少物品\([l,r]\)的dp数组arr. 然后solve(l,mid ...

最新文章

  1. JavaScript装饰器模式
  2. 机器学习笔记:线性回归
  3. go的各种import
  4. Linux--Tail命令
  5. 什么是JAP,什么是ORM,与hibernate的关系
  6. php 类定义抽象方法吗,如何理解php的抽象类跟抽象方法
  7. 一个iOS开发者的Flutter“历险记”
  8. android 获取设备的serialNumber和Mac地址
  9. 机器学习之---马尔可夫随机场的应用
  10. 数论基础——扩展欧几里得【详细】
  11. c语言二级选择题APP,C语言二级题库
  12. 通达信资金净流入公式_通达信指标公式:资金净流入,监控资金流入流出,分享...
  13. 怎样进入国外的游戏行业工作?
  14. Facebook内部高效工作指南
  15. RAID磁盘冗余技术
  16. GitHub简单入门教程
  17. java 2048思路_Java版2048
  18. LightGBM算法——广告收益回归预测模型
  19. 海思视频和QT的Colorkey显示模式
  20. 杰里之主动降噪与物理降噪的区别【篇】

热门文章

  1. 微波传声技术(Voice to skull technology)
  2. python写字_python 实现PIL模块在图片画线写字
  3. android 智能笔连接,在纸上写字可同步到手机和电脑的智能笔
  4. Visual C++实现贪吃蛇游戏项目实战三:核心算法设计与实现(附源码和资源 可用于大作业)
  5. 怎么制作切水果游戏呢?我用这种方式实现!
  6. Python爬虫怎么挣钱?6个Python爬虫赚钱方式,搞搞副业不是问题
  7. 品读张爱玲的一生的“母女劫”
  8. 计算机中汉字的编码课件,汉字的计算机编码.ppt
  9. 学习笔记2018-11-9 读论文Calibration and validation of a generic multisensor algorithm for mapping of total
  10. 软件测试中怎么分析性能的好坏,软件测试中性能测试结果分析