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 ...
随机推荐
- SpringBoot应用操作Rabbitmq(direct高级操作)
一.首先声明完成任务架构,通过direct订阅/发布的模式进行生产消费. a.消息生产指定交换器和路由key b.消费者绑定交换器,路由key和队列的关系(集群监控收到的消息不重复) 二.实战演练 1 ...
- centos7安装及部署zabbix监控
一:实验环境 server.zabbix.com-------------------- 192.168.200.11 agent.zabbix.com------------------------ ...
- 009.Ansible模板管理 Jinja2
一 Jinja2简介 Jinja2是基于python的模板引擎. 假设说现在我们需要一次性在10台主机上安装redis,这个通过playbook现在已经很容易实现.默认情况下,所有的redis安装完成 ...
- json序列化字符串后,配置枚举类型显示数值而不是名称
2019独角兽企业重金招聘Python工程师标准>>> 经常有这么一个需求,实体类里面用到枚举常量,但序列化成json字符串时.默认并不是我想要的值,而是名称,如下 类 @Data ...
- Rabbit and Turtle
知乎上有这样一个问题:如果兔子都在拼命奔跑,是什么给了作为乌龟的你前进的动力? 随着视野的不断开阔,我们难免怀疑自身前进的意义,曾经有很长一段时间我也深陷其中,直到看到余亦多先生的回答才有所感悟. 问 ...
- CodeForces - 1245A Good ol' Numbers Coloring (思维)
Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...
- MySQL 数据库赋权
1.进入数据库,查看数据库账户 # 进入数据库 mysql –u root –p ---> 输入密码... # 使用 mysql 库 use mysql; # 展示 mysql 库中所有表 sh ...
- tomcat多个springboot项目启动失败
多个springboot项目打包成war包并放到tomcat下运行时出错了错误信息: Caused by: org.springframework.jmx.export.UnableToRegiste ...
- Django 配置JWT认证方式
1. 安装 rest_framework + djangorestframework_simplejwt 安装djangorestframework_simplejwt :pip install dj ...
- spring学习笔记(七)HttpMessageConverter
spring学习笔记(七)HttpMessageConverter 1. HttpMessageConverter的加载 2. 从StringMessageConverter探究消息转换器的原理 1. ...