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 ...
随机推荐
- 创建和编辑 crontab 文件
http://docs.oracle.com/cd/E24847_01/html/819-6951/sysrescron-24589.html 创建和编辑 crontab 文件 创建 crontab ...
- [SQL Server系] -- 基本概念
以下是我总结的 SQL Server 数据库中的一些 基本概念,以便模糊时查询, 欢迎补充 1:主键: 概念: 数据表 经常有 一个列 或 列的组合,其值能唯一地标识表中的每一行.这样的一列或多列称 ...
- pycharm 基础教程
pycharm 教程(一)安装和首次使用 PyCharm 是我用过的python编辑器中,比较顺手的一个.而且可以跨平台,在macos和windows下面都可以用,这点比较好. 首先预览一下 PyCh ...
- 小米2S 连接Ubuntu Android Studio
1. 首先打开手机上的开发者选项,USB调试.拨号:*#*#717717#*#* ,手机会以Toast形式出现“……enable”字样.再次拨号可disable. 2. Ubuntu安装mtpfs: ...
- 欧拉工程第60题:Prime pair sets
题目链接 五个数,任意两个数的任意链接后的数还是质数 满足这个条件的最小五个数的和是多少? 结果:26033 纯暴力破解: package projecteuler51to60; import jav ...
- 机器学习 —— 概率图模型(Homework: Factors)
Talk is cheap, I show you the code 第一章的作业主要是关于PGM的因子操作.实际上,因子是整个概率图的核心.对于有向图而言,因子对应的是CPD(条件分布):对无向图而 ...
- java--面向抽象编程
所谓面向抽象编程是指当设计某种重要的类时,不让该类面向具体的类,而是面向抽象类,及所设计类中的重要数据是抽象类声明的对象,而不是具体类声明的对象.就是利用abstract来设计实现用户需求. 比如:我 ...
- windows/ubuntu 文件共享之 Samba 配置
很多时候需要在windows上和ubuntu 上共享文件,一直没怎么去找方法,得知Samba 可以实现在windows上访问linux的文件,这样一来要从windows文件放到linux中就方便了,听 ...
- Lucas定理学习小记
(1)Lucas定理:p为素数,则有: (2)证明: n=(ak...a2,a1,a0)p = (ak...a2,a1)p*p + a0 = [n/p]*p+a0,m=[m/p]*p+b0其次,我们 ...
- Setup Oracle 11gR2 for Redhat Linux AS 4 Update 7 x64
Setup Oracle 11gR2 for Redhat Linux AS 4 Update 7 x64 1. checking linux version. [root@localhost ~]# ...