poj2987 最大闭合权子图基础题
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 10905 | Accepted: 3291 |
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 【问题描述】
在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,
如果该下属还有下属,照斩不误。给出每个人的贡献值和从属关系,求在最大贡献值的前提下最小剩下多少人及最大贡献值。留下多少人无所谓,现在老板想知道留下的人最大的贡献值是多少。
【输入描述】
第一行两个整数n,m,表示有多少个员工与多少个从属关系。
第二行n个整数,表示每个员工的贡献值。
接着m行,每行两个数x,y,表示y是x的下属,一个员工可能有多个下属但不会有多个上司
【输出描述】
包括两个数,表示最大贡献值前提下最小剩下多少人及最大贡献值和。
【样例输入】:
5 5
8
-9
-20
12
-10
1 2
2 5
1 4
3 4
4 5
【样例输出】:
2 2
【其他说明】:
0 < n ≤ 5000
0 ≤ m ≤ 60000
员工价值≤107
样例中留下4,5号员工是最好情况 基础知识点这里
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=;
const int M=;
const long long INF=0x3f3f3f3f3f3f;
int head[N],tot,S,T;
int q[N],dis[N],n,m;
bool vis[N];
struct node
{
int next,v;
long long w;
} e[M<<];
void add(int u,int v,long long w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
bool bfs()
{
memset(dis,-,sizeof(dis));
dis[S]=;
int l=,r=;
q[r++]=S;
while(l<r)
{
int u=q[l++];
for(int i=head[u]; ~i; i=e[i].next)
{
int v=e[i].v;
if(dis[v]==-&&e[i].w>)
{
q[r++]=v;
dis[v]=dis[u]+;
if(v==T) return true;
}
}
}
return false;
}
long long dfs(int s,long long low)
{
if(s==T||!low) return low;
long long ans=low,a;
for(int i=head[s]; ~i; i=e[i].next)
{
if(e[i].w>&&dis[e[i].v]==dis[s]+&&(a=dfs(e[i].v,min(e[i].w,ans))))
{
e[i].w-=a;
e[i^].w+=a;
ans-=a;
if(!ans) return low;
}
}
if(low==ans) dis[s]=-;
return low-ans;
}
void dfs2(int &cnt,int u){
for(int i=head[u];~i;i=e[i].next){
if(!vis[e[i].v]&&e[i].w>) {
vis[e[i].v]=;
++cnt;
dfs2(cnt,e[i].v);
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
S=,T=n+;
int x,y;
long long ans=;
long long c;
memset(head,-,sizeof(head));
tot=;
for(int i=; i<=n; ++i)
{
scanf("%I64d",&c);
if(c>=) {add(S,i,c),add(i,S,);ans+=c;}
else add(i,T,-c),add(T,i,);
}
while(m--){
scanf("%d%d",&x,&y);
add(x,y,INF);
add(y,x,);
}
while(bfs()) ans-=dfs(S,INF);
memset(vis,,sizeof(vis));
vis[S]=;
int cnt=;
dfs2(cnt,);
printf("%d %I64d\n",cnt,ans);
}
}
poj2987 最大闭合权子图基础题的更多相关文章
- bzoj1497最大闭权图基础题
1497: [NOI2006]最大获利 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5485 Solved: 2661[Submit][Status] ...
- hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
hdu3879 base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点 ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- linux面试题-基础题1
第1章 基础题1 1.1 在装系统创建Linux分区时,一般至少需要创建两个分区( ) A.FAT.NTFS B. /usr.swap C. /boot.swap D.swap./ 1.2 ...
- Android测试基础题(三)
今天接着给大家带来的是Android测试基础题(三). 需求:定义一个排序的方法,根据用户传入的double类型数组进行排序,并返回排序后的数组 俗话说的好:温故而知新,可以为师矣 packag ...
- 小试牛刀3之JavaScript基础题
JavaScript基础题 1.让用户输入两个数字,然后输出相加的结果. *prompt() 方法用于显示可提示用户进行输入的对话框. 语法: prompt(text,defaultText) 说明: ...
- 小试牛刀2:JavaScript基础题
JavaScript基础题 1.网页中有个字符串“我有一个梦想”,使用JavaScript获取该字符串的长度,同时输出字符串最后两个字. 答案: <!DOCTYPE html PUBLIC &q ...
- nyist oj 79 拦截导弹 (动态规划基础题)
拦截导弹 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...
- Java面试题以及答案精选(架构师面试题)-基础题1
基础题 一.String,StringBuffer, StringBuilder 的区别是什么?String为什么是不可变的?1. String是字符串常量,StringBuffer和StringBu ...
随机推荐
- 从Spring迁移到Spring Boot
文章目录 添加Spring Boot starters 添加应用程序入口 Import Configuration和Components 迁移应用程序资源 迁移应用程序属性文件 迁移Spring We ...
- JDK11的重要新特性
文章目录 JDK11发布啦 Oracle不再提供JRE和Server JRE下载 删除部署工具 JavaFX不再包含在JDK中 删除Java EE和CORBA模块 JDK11发布啦 JDK11 在20 ...
- 【用AI开挂的人生】5岁学编程,12岁进IBM
" 跟普通孩子比起来,14岁少年Tanmay Bakshi身上有无数光环: 5岁开始对计算机感兴趣 9岁发布了第1个iOSAPP 12岁成为IBM沃森最年轻的程序员 现任IBM Watson ...
- centos6更换yum源和epel源
epel是yum的一个软件用源,包含了很多基本源中没有的软件,cobbler就属于基本源中没有的软件,所以需要安装epel源. yum源: wget -O /etc/yum.repos.d/CentO ...
- 发布AI芯片昆仑和百度大脑3.0、L4自动驾驶巴士量产下线,这是百度All in AI一年后的最新答卷...
机器之心报道,作者:李泽南. 去年的 7 月 5 日,百度在北京国际会议中心开办了首届「AI 开发者大会」.在会上,百度首次喊出了「All in AI」的口号.一年的时间过去了,今天在同样地点举行的第 ...
- 多线程高并发编程(7) -- Future源码分析
一.概念 A Future计算的结果. 提供方法来检查计算是否完成,等待其完成,并检索计算结果. 结果只能在计算完成后使用方法get进行检索,如有必要,阻塞,直到准备就绪. 取消由cancel方法执行 ...
- 《深入理解Java虚拟机》第 3 版里面到底多了哪些知识点?本文竟然得到了本书作者的认可!
这是why的第 47 篇原创文章 荒腔走板 大家好,我是 why.老规矩,先是简短的荒腔走板聊聊生活. 上面的图是前几天拍的,那天晚上下班后,刚刚走进小区就看到了这一轮弯月和旁边那一颗特别特别亮的星星 ...
- Mysql常用sql语句(21)- regexp 正则表达式查询
测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 正则的强大不言而喻,Mysql中也提供了 reg ...
- spark优化总结
1.Spark调优背景 目前Zeppelin已经上线一段时间,Spark作为底层SQL执行引擎,需要进行整体性能调优,来提高SQL查询效率.本文主要给出调优的结论,因为涉及参数很多,故没有很细粒度调优 ...
- MySQL安装(linux)
Centos 安装mysql 安装mariadb yum install mariadb mariadb-server mariadb-devel 安装mysql rpm -qa | grep MyS ...