POJ 2987 Firing 网络流 最大权闭合图
http://poj.org/problem?id=2987
https://blog.csdn.net/u014686462/article/details/48533253
给一个闭合图,要求输出其最大权闭合图的权值和需要选的最少点数,最大权闭合图定义和网络流连边方式见博客。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<queue>
using namespace std;
#define LL long long
const int maxn=;
const LL minf=(LL)1e14;
int n,m,s,t;
LL val[maxn]={};
struct nod{
int y,next;LL v;
}e[maxn*];
int head[maxn],tot=;
queue<int>q;
int dep[maxn]={},vis[maxn]={},id[maxn]={},tai=;
void init(int x,int y,LL v){
e[++tot].y=y;e[tot].v=v;e[tot].next=head[x];head[x]=tot;
}
bool dfs(){
memset(dep,,sizeof(dep));
q.push(s);dep[s]=;
while(!q.empty()){
int x=q.front();q.pop();
for(int i=head[x];i;i=e[i].next){
if(e[i].v&&!dep[e[i].y]){
dep[e[i].y]=dep[x]+;
q.push(e[i].y);
}
}
}
return dep[t];
}
LL dfs1(int x,LL fc){
if(x==t){
return fc;
}
LL he=,z;
for(int i=head[x];i;i=e[i].next){
if(dep[x]+==dep[e[i].y]){
z=dfs1(e[i].y,min(fc-he,e[i].v));
he+=z;e[i].v-=z;e[i^].v+=z;
if(he==fc)break;
}
}
return he;
}
void dfs2(int x){
if(x==t)return;
if(x!=s)id[++tai]=x;
vis[x]=;
for(int i=head[x];i;i=e[i].next){
if(e[i].v&&!vis[e[i].y]){
dfs2(e[i].y);
}
}
}
int main(){
int x,y; LL ans=;
scanf("%d%d",&n,&m);
s=n+;t=s+;
for(int i=;i<=n;i++){
scanf("%lld",&val[i]);
if(val[i]>=){init(s,i,val[i]);init(i,s,);ans=ans+val[i];}
else {init(i,t,-val[i]);init(t,i,);}
}
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
init(x,y,minf);init(y,x,);
}
while(dfs())ans-=dfs1(s,minf);
dfs2(s);
printf("%d ",tai);
printf("%lld\n",ans);
return ;
}
POJ 2987 Firing 网络流 最大权闭合图的更多相关文章
- POJ 2987 Firing(最大权闭合图)
[题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...
- POJ 2987 Firing【最大权闭合图-最小割】
题意:给出一个有向图,选择一个点,则要选择它的可以到达的所有节点.选择每个点有各自的利益或损失.求最大化的利益,以及此时选择人数的最小值. 算法:构造源点s汇点t,从s到每个正数点建边,容量为利益.每 ...
- POJ 2987 Firing (最大权闭合图)
Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12108 Accepted: 3666 Descript ...
- poj 2987 Firing【最大权闭合子图+玄学计数 || BFS】
玄学计数 LYY Orz 第一次见这种神奇的计数方式,乍一看非常不靠谱但是仔细想想还卡不掉 就是把在建图的时候把正权变成w*10000-1,负权变成w*10000+1,跑最大权闭合子图.后面的1作用是 ...
- POJ 2987:Firing(最大权闭合图)
http://poj.org/problem?id=2987 题意:有公司要裁员,每裁一个人可以得到收益(有正有负),而且如果裁掉的这个人有党羽的话,必须将这个人的所有党羽都裁除,问最少的裁员人数是多 ...
- BZOJ 1497 JZYZOJ 1344 [NOI2006]最大获利 网络流 最大权闭合图
http://www.lydsy.com/JudgeOnline/problem.php?id=1497 http://172.20.6.3/Problem_Show.asp?id=1344 思路 ...
- POJ2987 Firing 【最大权闭合图】
POJ2987 Firing Description You've finally got mad at "the world's most stupid" employees o ...
- Bzoj 1391: [Ceoi2008]order 网络流,最大权闭合图
1391: [Ceoi2008]order Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1105 Solved: 331[Submit][Statu ...
- poj 2987 Firing 最大权闭合图
题目链接:http://poj.org/problem?id=2987 You’ve finally got mad at “the world’s most stupid” employees of ...
随机推荐
- RW RO ZI ROM keil中的含义
编译的一个ARM的程序,会得到这样的信息: ============================================================================== ...
- MODULE_DEVICE_TABLE【转】
转自:http://blog.csdn.net/tangkegagalikaiwu/article/details/8444249 This pci_device_id structure needs ...
- Add custom daemon on Linux System
Ubuntu add custom service(daemon) Task 需要在系统启动的时候自动启动一个服务(后台程序),在系统关闭的时候关闭服务. 比如在部署某个应用之前,需要将某个任务设置成 ...
- Runtime.getRuntime().exec 类 防止阻塞
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; impor ...
- 缓存数据库-redis数据类型和操作(string)
Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合) 一:String(字符串) string是redis ...
- Isolate randomforest检测异常点的非监督方法
由于异常数据的两个特征(少且不同: few and different) 异常数据只占很少量; 异常数据特征值和正常数据差别很大. iTree的构成过程如下: l 随机选择一个属性Attr: l ...
- Bootstrap FileInput中文API文档
Bootstrap FileInput中文API整理 这段时间做项目用到bootstrap fileinput插件上传文件,在用的过程中,网上能查到的api都不是很全,所以想着整理一份比较详细的文档, ...
- VS Code折腾记 - (1)扯淡
题外话 距离上篇介绍VSCode的文章已经过去四十多天,已经在正式项目作为主力开发工具了. 社区的发展非常快速,更新迭代够快,功能基本已经满足我所需了: 这个系列教程基于最新的vs code 1.8. ...
- 20155309 2016-2017-2《Java程序设计》课程总结
预备作业1http://www.cnblogs.com/nhx19970709/p/6155580.html 第一次写博客,也是第一次用Markdown,具体流程都还不是很熟悉 预备作业2http:/ ...
- #CSS 文本溢出部分显示省略号
单行结尾溢出: 用text-overflow:ellipsis属性实现,当然还需要加宽度width属来兼容部分浏览. width:300px;overflow: hidden; text-overfl ...