Firing
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 10884   Accepted: 3286

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 ≤ 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

题意:

n个点m条边的有向图,每个点都有一个权值,要在其中找到一张子图,使得所有点权值和最大,输出点数和权值和

结论:最大流:

①如果a和b连有一条边(a->b),那么a到b连接一条流量为无穷大的边

②如果a点的权值为正,那么源点到a点连接一条容量为val[a]的边

②如果a点的权值为负,那么a点到汇点连接一条容量为-val[a]的边

最大权就是权值为正的点之和-最大流

最大权闭合子图就是残余网络中与源点相连的那一部分

这样一定不会出现非法情况,因为如果a到b有条边,且a点的权值为正b点的权值为负,假设你选了a没选b,那么相当于没有割掉S到a的边,也没有割掉b到T的边,这样S到T就是联通的了所以当前肯定不是最大流

这有一篇比较好的证明:

http://www.cnblogs.com/wuyiqi/archive/2012/03/12/2391960.html

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#define LL long long
using namespace std;
LL n, m, sum, cnt, S, T, val[5005], vis[5005], head[5005], h[5005], cur[5005];
typedef struct
{LL to, next;LL flow;
}Road;
Road G[160005];
void Add(LL u, LL v, LL flow)
{cnt++;G[cnt].next = head[u];head[u] = cnt;G[cnt].to = v;G[cnt].flow = flow;
}
int Jud()
{LL now, i;queue<int> q;memset(h, -1, sizeof(h));q.push(S);h[S] = 0;while(q.empty()==0){now = q.front();q.pop();for(i=head[now];i!=0;i=G[i].next){if(G[i].flow && h[G[i].to]==-1){h[G[i].to] = h[now]+1;q.push(G[i].to);}}}if(h[T]!=-1)return 1;return 0;
}
LL Sech(LL x, LL flow)
{LL w, used, i;if(x==T)return flow;used = 0;for(i=cur[x];i!=0;i=G[i].next){if(h[G[i].to]==h[x]+1){w = Sech(G[i].to, min(flow-used, G[i].flow));G[i].flow -= w;G[i^1].flow += w;if(G[i].flow)cur[x] = i;used += w;if(used==flow)return flow;}}if(used==0)h[x] = -1;return used;
}
LL Dinic()
{LL i, flow = 0;while(Jud()){for(i=S;i<=T;i++)cur[i] = head[i];flow += Sech(S, (LL)1<<50);}return flow;
}
void Sech(LL x)
{int i, v;sum++;vis[x] = 1;for(i=head[x];i!=0;i=G[i].next){v = G[i].to;if(G[i].flow>0 && vis[v]==0)Sech(v);}
}
int main(void)
{LL n, m, i, x, y, ans;while(scanf("%lld%lld", &n, &m)!=EOF){cnt = 1, ans = 0;S = 0, T = n+1;memset(head, 0, sizeof(head));for(i=1;i<=n;i++){scanf("%lld", &val[i]);if(val[i]>0){ans += val[i];Add(S, i, val[i]);Add(i, S, 0);}else{Add(i, T, -val[i]);Add(T, i, 0);}}for(i=1;i<=m;i++){scanf("%lld%lld", &x, &y);Add(x, y, (LL)1<<50);Add(y, x, 0);}memset(vis, 0, sizeof(vis));ans -= Dinic();sum = 0;Sech(S);printf("%lld %lld\n", sum-1, ans);}return 0;
}

最大权闭合子图(poj 2987 Firing)相关推荐

  1. poj 2987 Firing (最大权闭合子图)

    Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10248   Accepted: 3110 Descript ...

  2. poj 2987 Firing【最大权闭合子图+玄学计数 || BFS】

    玄学计数 LYY Orz 第一次见这种神奇的计数方式,乍一看非常不靠谱但是仔细想想还卡不掉 就是把在建图的时候把正权变成w*10000-1,负权变成w*10000+1,跑最大权闭合子图.后面的1作用是 ...

  3. 2018.06.27Firing(最大权闭合子图)

    Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 11558 Accepted: 3494 Description ...

  4. 最大权闭合子图(最小割)

    最大权闭合子图(最大流最小割) •参考资料 [1]最大权闭合子图 •权闭合子图 存在一个图的子图,使得子图中的所有点出度指向的点依旧在这个子图内,则此子图是闭合子图. 在这个图中有8个闭合子图:∅,{ ...

  5. 【转载】最大权闭合子图 【网络流】

    原文链接 定义 所谓闭合子图就是给定一个有向图,从中选择一些点组成一个点集V.对于V中任意一个点,其后续节点都仍然在V中. 建模 首先建立源点s和汇点t,将源点s与所有权值为正的点相连,容量为权值:将 ...

  6. Cogs 727. [网络流24题] 太空飞行计划(最大权闭合子图)

    [网络流24题] 太空飞行计划 ★★☆ 输入文件:shuttle.in 输出文件:shuttle.out 简单对比 时间限制:1 s 内存限制:128 MB [问题描述] W 教授正在为国家航天中心计 ...

  7. #1398 : 网络流五·最大权闭合子图

    题目链接 结论 最大权闭合子图的权值 === 所有正权点之和−-−最小割 简单割(或最小割)将有向图分成两类,与源点sss相连的称为SSS,与汇点ttt相连的称为TTT 割的流量===与sss相连的正 ...

  8. hihocoder1398 网络流五之最大权闭合子图

    最大权闭合子图 虽然我自己现在总结不好最大权闭合子图.但也算稍稍理解辣. 网络流起步ing~~~(- ̄▽ ̄)- #include<iostream> #include<cstdio& ...

  9. 中石油训练赛 - Trading Cards(最大权闭合子图)

    题目大意:给出 n 个卡片,可以自由买卖,且价格都是相同的,再给出 m 个集合,如果已经得到了其中一个集合中的卡片,那么可以获得该集合的收益,问如何操作可以使得收益最大化 题目分析:最大权闭合子图的模 ...

最新文章

  1. 管理“95后”,切记这3点
  2. pl sql如何调试oracle存储过程,PL/SQL Developer中调试oracle的存储过程
  3. vba excel 开发游戏_为什么要学习VBA?
  4. python jupyter安装_python之jupyter的安装
  5. python是面向对象的语言_*Python面向对象总结
  6. MySQL常用数据类型
  7. 启动Eclipse时发生An internal error occurred during: Initializing Java Tooling错误,详细提示如下:...
  8. git 还原文件到其他版本_如何在Git中还原旧文件版本
  9. 系统学习 TypeScript(一)——认识 TypeScript
  10. 测试局域网路的MTU最大值
  11. Ubuntu18.04笔记本触控板失效解决办法
  12. iPhone 手势识别
  13. Palantir的研究到金融风控(二)
  14. windows下文件名太长无法删除的问题
  15. ValueError: Cannot have number of splits n_splits=10 greater than the number of samples: n_samples=0
  16. Altium中PCB板上开窗
  17. diskgenius系统迁移_关于系统迁移至SSD
  18. 计算机是如何执行程序的
  19. python的print输出为红色以及其他颜色的代码示例,Python print()输出文字带颜色设置方法
  20. 如何编写 TypeScript 声明文件

热门文章

  1. 学python能干什么工作-什么是Python?学完之后能做哪些工作?
  2. python中文视频教程-中谷教育python中文视频教程(python视频教程) 完整版
  3. python教程推荐-关于推荐系统的详细介绍
  4. python代码大全-python贪吃蛇游戏代码
  5. Android基于讯飞语音SDK实现语音识别
  6. Hashmap存储大小
  7. webpack2.7.0配置不同的打包环境
  8. 最简单的基于FFmpeg的移动端例子:Android 视频解码器-单个库版
  9. Media Player Classic - HC 源代码分析 1:整体结构
  10. VC2010 编译 Media Player Classic - Home Cinema (mpc-hc)