POJ2987 Firing 最大权闭合图
详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html
值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部分和汇点相连
我们只需要关注和起点相连的点的点就好,如何统计呢?
只需要从起点开始搜索,只要边不是满流,一直搜就好
然后,答案就是总权值-最小割
注:对于dinic网络流,我还是喜欢LRJ白书上的,用起来方便
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N = ;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
const int maxn=5e3+;
struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int c,int d):from(u),to(v),cap(c),flow(d) {}
};
struct dinic
{
int s,t;
vector<Edge>edges;
vector<int>G[maxn];
int d[maxn];
int cur[maxn];
bool vis[maxn];
void init(){
for(int i=;i<maxn;++i)G[i].clear();
edges.clear();
}
bool bfs()
{
memset(vis,,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=;
vis[s]=;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=; i<G[x].size(); i++)
{
Edge &e= edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==)return a;
int flow=,f;
for(int &i=cur[x]; i<G[x].size(); i++)
{
Edge &e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow))))
{
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==)break;
}
}
return flow;
}
LL maxflow(int s,int t)
{
this->s=s;
this->t=t;
LL flow=;
while(bfs())
{
memset(cur,,sizeof(cur));
flow+=dfs(s,INF);
}
memset(vis,false,sizeof(vis));
return flow;
}
void addedge(int u,int v,int c)
{
Edge x(u,v,c,),y(v,u,,);
edges.push_back(x);
edges.push_back(y);
int l=edges.size();
G[u].push_back(l-);
G[v].push_back(l-);
}
int search(int u){
int ret=;vis[u]=true;
for(int i=;i<G[u].size();++i){
Edge &e=edges[G[u][i]];
if(vis[e.to]||e.flow==e.cap)continue;
ret+=search(e.to);
}
return ret;
}
}solve;
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
solve.init();LL sum=;
for(int i=;i<=n;++i){
int w;scanf("%d",&w);
if(w>)sum+=w,solve.addedge(,i,w);
else if(w<)solve.addedge(i,n+,-w);
}
for(int i=;i<m;++i){
int u,v;scanf("%d%d",&u,&v);
solve.addedge(u,v,INF);
}
LL mxf=solve.maxflow(,n+);
printf("%d %I64d\n",solve.search()-,sum-mxf);
}
return ;
}
POJ2987 Firing 最大权闭合图的更多相关文章
- poj2987 Firing 最大权闭合子图 边权有正有负
/** 题目:poj2987 Firing 最大权闭合子图 边权有正有负 链接:http://poj.org/problem?id=2987 题意:由于金融危机,公司要裁员,如果裁了员工x,那么x的下 ...
- poj 2987 Firing 最大权闭合图
题目链接:http://poj.org/problem?id=2987 You’ve finally got mad at “the world’s most stupid” employees of ...
- POJ2987 Firing 【最大权闭合图】
POJ2987 Firing Description You've finally got mad at "the world's most stupid" employees o ...
- POJ 2987 Firing(最大流最小割の最大权闭合图)
Description You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do ...
- POJ 2987:Firing(最大权闭合图)
http://poj.org/problem?id=2987 题意:有公司要裁员,每裁一个人可以得到收益(有正有负),而且如果裁掉的这个人有党羽的话,必须将这个人的所有党羽都裁除,问最少的裁员人数是多 ...
- POJ 2987 Firing【最大权闭合图-最小割】
题意:给出一个有向图,选择一个点,则要选择它的可以到达的所有节点.选择每个点有各自的利益或损失.求最大化的利益,以及此时选择人数的最小值. 算法:构造源点s汇点t,从s到每个正数点建边,容量为利益.每 ...
- POJ 2987 Firing 网络流 最大权闭合图
http://poj.org/problem?id=2987 https://blog.csdn.net/u014686462/article/details/48533253 给一个闭合图,要求输出 ...
- POJ 2987 Firing(最大权闭合图)
[题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...
- poj 2987 最大权闭合图
Language: Default Firing Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 8744 Accept ...
随机推荐
- hdu1022 Train Problem I
http://acm.hdu.edu.cn/showproblem.php?pid=1022 #include<iostream> #include<stdio.h> #inc ...
- mysql小记--基础知识
一.事务 事务是由一组SQL语句组成的逻辑处理单元. 事务的特征ACID,即原子性.一致性.隔离性和持久性. 原子性(Atomicity)事务作为整体执行,操作要么全部执行.要么全部不执行. 一致性( ...
- 转:UGUI与NGUI的区别与优缺点
1. NGUI与UGUI的区别 1) uGUI的Canvas 有世界坐标和屏幕坐标 2) uGUI的Image可以使用material 3) UGUI通过Mask来裁剪,而NGUI通过Pa ...
- javaWEB邮件测试
新建一个工具类: Mail.java 该类的主要关键点是:1.设置系统属性.也就是你是用什么协议来进行邮件发送的,邮件协议有很多在种,比如impt,smpt,prop等协议, 我现在测试用的是smpt ...
- 2410中断中SRCPND和INTPND清零的疑问
2410中断中SRCPND和INTPND清零的疑问SRCPND是中断源引脚寄存器,某个位被置1表示相应的中断被触发,但我们知道在同一时刻内系统可以触发若干个中断,只要中断被触发了,SRCPND的相应位 ...
- javascript基础之数组对象
一.定义数组的方法: 定义了一个空数组: var myArray =new Array(); 指定有n个空元素的数组: var myArray=new Array(n); 定义数组并赋值: var m ...
- C#基础练习(事件登陆案例)
Form1的后台代码: namespace _08事件登陆案例 { public partial class Form1 : Form { public Form1() ...
- Android 给listview设置分割线与边界的距离
ListView可通过配置 android:dividerline android:dividerHeight 来实现分割listview中item的效果,如图: 如果想跟上图一样实现这种有设计的分割 ...
- 4、JPA table主键生成策略(在JPA中table策略是首推!!!)
用 table 来生成主键详解 它是在不影响性能情况下,通用性最强的 JPA 主键生成器.这种方法生成主键的策略可以适用于任何数据库,不必担心不同数据库不兼容造成的问题. initialValue不起 ...
- 利用PC创建一个无线接入点
win7 创建虚拟接入点,修改接入点名称和密码,然后存为bat文件,以管理员模式运行 netsh wlan set hostednetwork mode=allow ssid=APName key=p ...