描述


http://poj.org/problem?id=2987

要炒员工鱿鱼,炒了一个人,他的下属一定被炒.给出每个人被炒后公司的收益(负值表示亏损),问怎样炒公司收益最大,以及这种方法炒了几个人.(先输出人数)

Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 9674   Accepted: 2906

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.

Source

分析


求最大收益是用最大权闭合图.要炒一个人,他的下属也要被炒,那么边由上司连向下属,求最大权闭合图即可.

求炒了几个人实际是就是求先前求出来的那个最大权闭合图中点的个数.最大权闭合图对应的是最小割,而最小割中的边都是满流的,所以从源点出发无法到达闭合图的补集(即T),并且由于是闭合图,所以图中的边都不属于割,这样闭合图中就没有流量,从源点出发可以到达闭合图中的每一个点.

注意:

1.要用long long.

ps.f[S,T]=|f|,即整个图的流等于流过割的流,这样这道题中,f=fmax=cmin,又f=f[S,T],所以f[S,T]=cmin,也就是流过最小割的流等于最小割的容量,所以满流了.

 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define rep(i,n) for(int i=0;i<(n);i++)
#define for1(i,a,n) for(int i=(a);i<=(n);i++)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getnum()
#define ll long long
using namespace std; const int maxn=+,INF=0x7fffffff;
int n,m;
ll sumw;
int level[maxn],iter[maxn];
bool vis[maxn];
struct edge
{
int to,cap,rev;
edge() {}
edge(int a,ll b,int c) : to(a),cap(b),rev(c) {}
};
vector <edge> g[maxn]; inline int getnum()
{
int r=,k=; char c;
for(c=getchar();c<''||c>'';c=getchar()) if(c=='-') k=-;
for(;c>=''&&c<='';c=getchar()) r=r*+c-'';
return r*k;
} void add_edge(int from,int to,int cap)
{
g[from].push_back(edge(to,cap,g[to].size()));
g[to].push_back(edge(from,,g[from].size()-));
} void bfs(int s)
{
CC(level,-);
level[s]=;
queue <int> q;
q.push(s);
while(!q.empty())
{
int t=q.front(); q.pop();
rep(i,g[t].size())
{
edge e=g[t][i];
if(e.cap>&&level[e.to]<)
{
level[e.to]=level[t]+;
q.push(e.to);
}
}
}
} int dfs(int v,int t,int 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[e.to]>level[v])
{
int 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=;
bfs(s);
while(level[t]>)
{
int f;
CC(iter,);
while((f=dfs(s,t,INF))>) flow+=f;
bfs(s);
}
return flow;
} int DFS(int v)
{
vis[v]=true;
int ans=;
rep(i,g[v].size())
{
edge e=g[v][i];
if(!vis[e.to]&&e.cap>) ans+=DFS(e.to);
}
return ans;
} void init()
{
read(n); read(m);
for1(i,,n)
{
int w; read(w);
if(w>)
{
sumw+=w;
add_edge(,i,w);
}
else
{
add_edge(i,n+,-w);
}
}
for1(i,,m)
{
int a,b; read(a); read(b);
add_edge(a,b,INF);
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("firing.in","r",stdin);
freopen("firing.out","w",stdout);
#endif
init();
ll ans=sumw-max_flow(,n+);
printf("%d %lld\n",DFS()-,ans);
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
system("firing.out");
#endif
return ;
}
 

POJ_2987_Firing_(最大流+最大权闭合图)的更多相关文章

  1. 【TYVJ】1338 QQ农场(最大流+最大权闭合图)

    http://tyvj.cn/Problem_Show.aspx?id=1338 时间才排到rank7,还不快啊囧.isap我常数都写得那么小了... 最大权闭合图看我另一篇博文吧 此题很明显的模型. ...

  2. BZOJ_1565_[NOI2009]_植物大战僵尸_(Tarjan+最大流+最大权闭合图)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1565 n*m的矩阵,可以种植植物,僵尸从图的右边进入吃植物.前面的植物可以保护后面的植物,还有 ...

  3. BZOJ_1497_[NOI2006]_最大获利_(最大流+最大权闭合图)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1497 共n个站点,给出建立每个站点所需要的花费.现在有m个客户需要开通服务,每个客户需要有两个 ...

  4. [网络流24题] 太空飞行计划问题 (最大流->最大权闭合图)

    洛谷传送门 LOJ传送门 做这道题之前建议先看这篇论文,虽然论文里很多地方用了很多术语,但hbt神犇讲得很明白 这篇题解更加偏向于感性理解 把问题放到二分图上,左侧一列点是实验,权值为$p[i]$,右 ...

  5. [网络流24题] 方格取数问题/骑士共存问题 (最大流->最大权闭合图)

    洛谷传送门 LOJ传送门 和太空飞行计划问题一样,这依然是一道最大权闭合图问题 “骑士共存问题”是“方格取数问题”的弱化版,本题解不再赘述“骑士共存问题”的做法 分析题目,如果我们能把所有方格的数都给 ...

  6. [luoguP2762] 太空飞行计划问题(最大权闭合图—最小割—最大流)

    传送门 如果将每一个实验和其所对的仪器连一条有向边,那么原图就是一个dag图(有向无环) 每一个点都有一个点权,实验为收益(正数),仪器为花费(负数). 那么接下来可以引出闭合图的概念了. 闭合图是原 ...

  7. poj 2987 最大权闭合图

    Language: Default Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 8744   Accept ...

  8. 最大权闭合图 && 【BZOJ】1497: [NOI2006]最大获利

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 最大权闭合图详细请看胡伯涛论文<最小割模型在信息学竞赛中的应用>,我在这里截图它的 ...

  9. 最大权闭合图hdu3996

    定义:最大权闭合图:是有向图的一个点集,且该点集的所有出边都指向该集合.即闭合图内任意点的集合也在改闭合图内,给每个点分配一个点权值Pu,最大权闭合图就是使闭合图的点权之和最大. 最小割建边方式:源点 ...

随机推荐

  1. 转载--SQL Server 2005的XQuery介绍

    原文地址: http://bbs.51cto.com/thread-458009-1-1.html   引用: 摘要 本文介绍了SQL Server 2005能够支持的XQuery的各方面特性如FLW ...

  2. 10个你可能不知道的JavaScript小技巧

    1.变量转换 看起来很简单,但据我所看到的,使用构造函数,像Array()或者Number()来进行变量转换是常用的做法.始终使用原始数据类型(有时也称为字面量)来转换变量,这种没有任何额外的影响的做 ...

  3. 初尝seajs,只提供自己学习做笔记

    (仅供自己使用,勿喷) 闲着无聊,尝试下seajs, 只是在公司项目上随便添加并测试了一下,做下记录, 方便以后自己使用更快的上手: 下载最新的sea.js, v- 3.0.0 新建seajsConf ...

  4. Bzoj 2431 HAOI2009 逆序对数列

    Description 对于一个数列{ai},如果有i**<**j且ai>aj,那么我们称ai与aj为一对逆序对数.若对于任意一个由1~n自然数组成的数列,可以很容易求出有多少个逆序对数. ...

  5. Cocos2d-x 3.0坐标系详解(转载)

    Cocos2d-x 3.0坐标系详解 Cocos2d-x坐标系和OpenGL坐标系相同,都是起源于笛卡尔坐标系. 笛卡尔坐标系 笛卡尔坐标系中定义右手系原点在左下角,x向右,y向上,z向外,OpenG ...

  6. 文件打开方式O_DSYNC、O_RSYNC、O_SYNC

    O_DSYNC: 每次write都等待物理I/O完成,但是如果写操作不影响读取刚写入的数据,则不等待文件属性更新 O_RSYNC: 每个以文件描述符作为参数的read操作等待,直到所有对文件同一部分的 ...

  7. MySQL的基本使用

    SQL     DDL:数据定义语言  CREATE DROP ALTER     DML:数据操作语言  SELECT INSERT UPDATE DELETE     DCL:数据控制语言  GR ...

  8. 【原创】QT编程 多线程

    请先保证已安装QT,没有请参考 http://www.cnblogs.com/kavs/p/4608926.html  安装QT. 新建threads文件夹存放项目:mkdir threads sud ...

  9. UEditor富文本编辑框学习

    1.首先需要引入CSS.JS <!--富文本编辑框--> <link href="${pageContext.request.contextPath}/css/plugin ...

  10. 犯这个错误的肯定不止我一个 关于File

    File.Create(string filePath)这种用法所有人都知道,这两天用到的时候却发现一个问题. 需要先判断文件是否存在,如果不存在则创建文件,然后向该文件写入数据,后续定时Append ...