描述


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. (转)C#中的Dictionary字典类介绍

    关键字:C# Dictionary 字典 作者:txw1958原文:http://www.cnblogs.com/txw1958/archive/2012/11/07/csharp-dictionar ...

  2. 【转】字符编码笔记:ASCII,Unicode和UTF-8

    今天整理笔记,关于NSString转NSData时,什么时候使用NSUTF8StringEncoding,或者NSASCIIStringEncoding,或者 NSUnicodeStringEncod ...

  3. 控制器的跳转-modal与push

    一.modal与pushmodal从下面往上盖住原来的控制器,一般上一个控制器和下一个控制器没有什么关联时用modal,比如联系人的加号跳转页面,任何控制器都可以用modal push一般是上下文有关 ...

  4. java之内存管理

    对于JVM的垃圾回收机制来说,是否回收一个对象的标准在于:是否还有引用变量引用该对象,只要有引用变量引用该对象,垃圾回收机制就不会回收它. 强引用:创建一个对象,并把这个对象赋给一个引用变量.这种引用 ...

  5. 国庆第六日(2014年10月6日11:51:15),node-webkit,理财产品

    (1)node-webkit:一篇很好的入门文章.入门.系列. 在window下的打包和运行.大漠的一篇讲解文章 . (2)lighttable: 官网. (3)现在的理财产品,雨后春笋般冒出:宝点网 ...

  6. 增加Swap内存

    增加512M Swap内存 #首先先建立一个分区,采用dd命令如下:dd if=/dev/zero of=/home/swap bs=1024 count=524288#(1个block为1K,512 ...

  7. HBuilder手机Iphone运行提示“未受信用的企业级开发者”

    HBuilder手机Iphone运行提示“未受信用的企业级开发者” 解决方法:设置-----通用------设备管理-----点击Digtial Heaven....---信任"Digtia ...

  8. O_NONBLOCK模式下写fifo的注意事项

    后台网络通信框架一般采用fifo来作为事件通知的机制:创建一个fifo,然后以非阻塞读和非阻塞写的方式打开fifo,然后把fd加到epoll里面,作为通知网络事件的fd. 在这里有个隐晦的问题容易被忽 ...

  9. jQuery DataTables 插件使用笔记

    初始化 在页面中 <!DOCTYPE html> <html> <head> <link rel="stylesheet" type=&q ...

  10. ubuntu 基本操作(1)

    命令行界面与图形界面 全屏模式: ctrl + alt + f1进入命令行界面 ctrl + alt + f7 切换图形界面 小窗口: ctrl+alt+t:进入终端 设计ubuntu root 密码 ...