poj 2987 最大权闭合图

时间:2022-09-12 18:14:19
Language:
Default
Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 8744   Accepted: 2631

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 individuallybi (|bi| ≤ 107, 1 ≤ i ≤ n). The remaining m lines each contain two integers i and j (1 ≤ ij ≤ 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

As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.
 
很基础的最大权闭合图
 
 #include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<climits>
#define MAXE 65100*2
#define MAXP 5100
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
using namespace std;
struct Edge
{
long long int s,t,next;
long long f;
} edge[MAXE];
long long int head[MAXP];
long long int cur[MAXP];
long long int pre[MAXP];
long long int stack[MAXE];
long long int dep[MAXP];
long long int ent;
long long int n,m,s,t,cot;
void add(long long int start,long long int last,long long int f)
{
edge[ent].s=start;
edge[ent].t=last;
edge[ent].f=f;
edge[ent].next=head[start];
head[start]=ent++;
edge[ent].s=last;
edge[ent].t=start;
edge[ent].f=;
edge[ent].next=head[last];
head[last]=ent++;
}
bool bfs(long long int S,long long int T)
{
memset(pre,-,sizeof(pre));
pre[S]=;
queue<long long int>q;
q.push(S);
while(!q.empty())
{
long long int temp=q.front();
q.pop();
for(long long int i=head[temp]; i!=-; i=edge[i].next)
{
long long int temp2=edge[i].t;
if(pre[temp2]==-&&edge[i].f)
{
pre[temp2]=pre[temp]+;
q.push(temp2);
}
}
}
return pre[T]!=-;
}
long long int dinic(long long int start,long long int last)
{
long long int flow=,now;
while(bfs(start,last))
{
long long int top=;
memcpy(cur,head,sizeof(head));
long long int u=start;
while()
{
if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流
{
long long int minn=INT_MAX;
for(long long int i=; i<top; i++)
{
if(minn>edge[stack[i]].f)
{
minn=edge[stack[i]].f;
now=i;
}
}
flow+=minn;
for(long long int i=; i<top; i++)
{
edge[stack[i]].f-=minn;
edge[stack[i]^].f+=minn;
}
top=now;
u=edge[stack[top]].s;
}
for(long long int i=cur[u]; i!=-; cur[u]=i=edge[i].next) //找出从u点出发能到的边
if(edge[i].f&&pre[edge[i].t]==pre[u]+)
break;
if(cur[u]==-)//如果从该点未找到可行边,将该点标记并回溯
{
if(top==)break;
pre[u]=-;
u=edge[stack[--top]].s;
}
else//如果找到了继续运行
{
stack[top++]=cur[u];
u=edge[cur[u]].t;
}
}
}
return flow;
}
void dfs(long long int u)
{
cot++;
dep[u]=;
for(long long int i=head[u];i!=-;i=edge[i].next)
{
long long int v=edge[i].t;
if(!dep[v]&&edge[i].f>)
{
dfs(v);
}
}
}
int main()
{
while(~scanf("%lld%lld",&n,&m))
{
s=;
t=n+;
ent=;
long long int cost,u,v;
long long int ans=;
memset(head,-,sizeof(head));
for(int i=; i<=n; i++)
{
scanf("%lld",&cost);
if(cost>)
{
add(s,i,cost);
ans+=cost;
}
else add(i,t,-cost);
}
for(int i=; i<=m; i++)
{
scanf("%lld%lld",&u,&v);
add(u,v,INT_MAX);
}
memset(dep,,sizeof(dep));
long long int flow=dinic(s,t);
cot=;
dfs(s);
printf("%lld ",cot-);
printf("%lld\n",ans-flow);
}
return ;
}

poj 2987 最大权闭合图的更多相关文章

  1. poj 2987&lpar;最大权闭合图&plus;割边最少&rpar;

    题目链接:http://poj.org/problem?id=2987 思路:标准的最大权闭合图,构图:从源点s向每个正收益点连边,容量为收益:从每个负收益点向汇点t连边,容量为收益的相反数:对于i是 ...

  2. hdu 2987最大权闭合图模板类型题

    /* 最大权闭合图模板类型的题,考验对知识概念的理解. 题意:如今要辞退一部分员工.辞退每个员工能够的到一部分利益(能够是负的),而且辞退员工,必须辞退他的下属.求最大利益和辞退的最小人数. 最大权闭 ...

  3. poj 2987 Firing 最大权闭合图

    题目链接:http://poj.org/problem?id=2987 You’ve finally got mad at “the world’s most stupid” employees of ...

  4. POJ 2987 Firing 网络流 最大权闭合图

    http://poj.org/problem?id=2987 https://blog.csdn.net/u014686462/article/details/48533253 给一个闭合图,要求输出 ...

  5. POJ 2987 Firing(最大权闭合图)

    [题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...

  6. POJ 2987:Firing(最大权闭合图)

    http://poj.org/problem?id=2987 题意:有公司要裁员,每裁一个人可以得到收益(有正有负),而且如果裁掉的这个人有党羽的话,必须将这个人的所有党羽都裁除,问最少的裁员人数是多 ...

  7. POJ 2987 Firing【最大权闭合图-最小割】

    题意:给出一个有向图,选择一个点,则要选择它的可以到达的所有节点.选择每个点有各自的利益或损失.求最大化的利益,以及此时选择人数的最小值. 算法:构造源点s汇点t,从s到每个正数点建边,容量为利益.每 ...

  8. POJ 2987 Firing(最大流最小割の最大权闭合图)

    Description You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do ...

  9. POJ 3155 Hard Life 最大密度子图 最大权闭合图 网络流 二分

    http://poj.org/problem?id=3155 最大密度子图和最大权闭合图性质很相近(大概可以这么说吧),一个是取最多的边一个是取最多有正贡献的点,而且都是有选一种必须选另一种的限制,一 ...

随机推荐

  1. Python 资源大全中文版

    Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-python 是 vinta 发起维护的 Python 资源列 ...

  2. Visual Studio 2013 Update 2 RTM 发布

    今天,微软再Visual Studio Blog发布了开放Visual Studio 2013 Update 2 RTM 下载的文章. 原来安装RC版本的同志们可以直接安装,提供在线安装和ISO下载安 ...

  3. 多线程学习中的Tips

    ParameterizedThreadStart委托与ThreadStart委托,非常相似,但ParameterizedThreadStart委托是面向带参数方法的.注意ParameterizedTh ...

  4. Laravel 安装多国语言包后,phpstorm 还是报错

    问题: 解决办法: vagrant@homestead:~/Code/awbeci$ composer require "overtrue/laravel-lang:~3.0" 总 ...

  5. hadoop错误Cannot load libsnappy&period;so&period;1 &lpar;libsnappy&period;so&period;1 cannot open shared object file No such file or directory&rpar;&excl;

    报如下错误 解决方法: 1.下载libsnappy.so.1(https://yunpan.cn/cSHRHTBJGVVX6  访问密码 c992) 2.上传到linux系统 3.安装 4.安装完成后 ...

  6. python之json

    import json import requests re = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=成都') re.en ...

  7. 大白菜U盘启动制作工具装机维护版V5&period;0–大白菜U盘下载中心

    大白菜U盘启动制作工具装机维护版V5.0–大白菜U盘下载中心   大白菜U盘启动制作工具装机维护版V5.0

  8. glup&sol;grunt&comma;browserify &sol; webpack等的比较

    gulp/grunt的比较: Gulp / Grunt 是一种工具,能够优化前端工作流程.比如自动刷新页面.combo.压缩css.js.编译less等等.简单来说,就是使用Gulp/Grunt,然后 ...

  9. &lbrack;洛谷P1842&rsqb; 奶牛玩杂技

    题目类型:贪心+证明,经典题 传送门:>Here< 题意:有\(N\)头奶牛,每个奶牛有一个重量\(W[i]\),力量\(S[i]\).定义每个奶牛的压扁程度为排在它前面的所有奶牛的总量之 ...

  10. Angular7&period;1&period;4&plus;Typescript3&period;1框架学习(一)

    起因:学习ionic4之前先学习下angular+ts 以win10为开发平台:当前最新版本为angular7;根据官网资料做如下总结: 1. angular安装 前提:Node.js 的 8.x 或 ...