POJ_2987_Firing_(最大流+最大权闭合图)
描述
http://poj.org/problem?id=2987
要炒员工鱿鱼,炒了一个人,他的下属一定被炒.给出每个人被炒后公司的收益(负值表示亏损),问怎样炒公司收益最大,以及这种方法炒了几个人.(先输出人数)
| 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 ≤ 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
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_(最大流+最大权闭合图)的更多相关文章
- 【TYVJ】1338 QQ农场(最大流+最大权闭合图)
http://tyvj.cn/Problem_Show.aspx?id=1338 时间才排到rank7,还不快啊囧.isap我常数都写得那么小了... 最大权闭合图看我另一篇博文吧 此题很明显的模型. ...
- BZOJ_1565_[NOI2009]_植物大战僵尸_(Tarjan+最大流+最大权闭合图)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1565 n*m的矩阵,可以种植植物,僵尸从图的右边进入吃植物.前面的植物可以保护后面的植物,还有 ...
- BZOJ_1497_[NOI2006]_最大获利_(最大流+最大权闭合图)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1497 共n个站点,给出建立每个站点所需要的花费.现在有m个客户需要开通服务,每个客户需要有两个 ...
- [网络流24题] 太空飞行计划问题 (最大流->最大权闭合图)
洛谷传送门 LOJ传送门 做这道题之前建议先看这篇论文,虽然论文里很多地方用了很多术语,但hbt神犇讲得很明白 这篇题解更加偏向于感性理解 把问题放到二分图上,左侧一列点是实验,权值为$p[i]$,右 ...
- [网络流24题] 方格取数问题/骑士共存问题 (最大流->最大权闭合图)
洛谷传送门 LOJ传送门 和太空飞行计划问题一样,这依然是一道最大权闭合图问题 “骑士共存问题”是“方格取数问题”的弱化版,本题解不再赘述“骑士共存问题”的做法 分析题目,如果我们能把所有方格的数都给 ...
- [luoguP2762] 太空飞行计划问题(最大权闭合图—最小割—最大流)
传送门 如果将每一个实验和其所对的仪器连一条有向边,那么原图就是一个dag图(有向无环) 每一个点都有一个点权,实验为收益(正数),仪器为花费(负数). 那么接下来可以引出闭合图的概念了. 闭合图是原 ...
- poj 2987 最大权闭合图
Language: Default Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8744 Accept ...
- 最大权闭合图 && 【BZOJ】1497: [NOI2006]最大获利
http://www.lydsy.com/JudgeOnline/problem.php?id=1497 最大权闭合图详细请看胡伯涛论文<最小割模型在信息学竞赛中的应用>,我在这里截图它的 ...
- 最大权闭合图hdu3996
定义:最大权闭合图:是有向图的一个点集,且该点集的所有出边都指向该集合.即闭合图内任意点的集合也在改闭合图内,给每个点分配一个点权值Pu,最大权闭合图就是使闭合图的点权之和最大. 最小割建边方式:源点 ...
随机推荐
- VB,VBS,VBA,ASP可引用的库参考
文件系统对象相关: ("SCRIPTING.FILESYSTEMOBJECT") 字典相关: ("SCRIPTING.DICTIONARY") 脚本外壳相关: ...
- 10.20_wiki
XWiki:官网.Documentation.User's GuideProgrammer's GuideAdministrator's Guide Developer's Guide (1) htt ...
- Codevs 1066 引水入城 2010年NOIP全国联赛提高组
1066 引水入城 2010年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description 在一个遥远的国度 ...
- atc游戏bot
游戏玩起来太虐心了,就写了个bot来玩, 代码在此: git clone https://github.com/chenfengyuan/atc-bot-new.gitgit clone https: ...
- tomcat server.xml配置文件 解析
把服务拆分出来了. 前几天我也进行了拆分.可是当时服务起不来所以我想会不会有什么设置,使得这个服务在主机中只能启一个.然后我又找了一台服务器,也把代码放了进去.结果仿佛是我料想到的样子, ...
- redis php 实例
redis php 实例一 redis的操作很多的,以前看到一个比较全的博客,但是现在找不到了.查个东西搜半天,下面整理一下php处理redis的例子,个人觉得常用一些例子.下面的例子都是基于php- ...
- Traveller数据访问路径
2015年10月数据访问路径
- php精粹-编写高效的php代码 --- php设计模式
1.选择一个最合适的设计模式 没有任何事物是完美的,也没有人说过设计模式一个严格的放之四海而皆准的解决方法.因此你可以改变这些模式,使它们更适合手头的工作.对于某些设计模式而言,他们就是所属程序固有的 ...
- C#正则表达式Regex类使用
作为文本处理的利器——Perl语言对正则表达式的最强大支持起到了重要的作用,正因为如此,许多其他语言在加入正则表达式引擎的时候都会或多或少的兼顾perl风格的正则表达式,开发出相应的引擎.本人使用pe ...
- MOS管体二极管的作用
这里有两种解释: 1.mos管本身自带有寄生二极管,作用是防止VDD过压的情况下,烧坏mos管,因为在过压对MOS管造成破坏之前,二极管先反向击穿,将大电流直接到地,从而避免MOS管被烧坏. 2.防止 ...