poj2987 求最大权闭合回路
建图差不多和以前做的差不多,就是最后询问这个闭合子图有多少个的时候,只要输出这个图的S集合,就是进行dfs能遍历到的点一定在S集合中,不能遍历到的点在T集合中
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int maxn=;
const long long INF=1LL<<;
typedef long long LL;
struct Dinic
{ struct Edge{
LL from,to,cap,flow;
Edge(LL cfrom=,LL cto=,LL ccap=,LL cflow=)
{
from=cfrom; to=cto; cap=ccap; flow=cflow;
}
};
int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n)
{
m=;
this->n=n;
for(int i=; i<=n; i++)G[i].clear();
edges.clear();
}
void addEdge(LL from,LL to, LL cap)
{
edges.push_back(Edge(from,to,cap,) );
edges.push_back(Edge(to,from,,));
m+=;
G[from].push_back(m-);
G[to].push_back(m-);
}
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]==false&&e.cap>e.flow)
{
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
}
LL DFS(int x,LL a)
{
if(x==t||a==)return a;
LL flow=,f;
for(int &i=cur[x]; i<G[x].size(); i++)
{
Edge &e=edges[G[x][i]];
LL dd =min(a,e.cap-e.flow);
if(d[x]+==d[e.to]&&(f=DFS(e.to,dd))>)
{
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);
}
return flow;
}
void find(int cur)
{
vis[cur]=true;
for(int i=; i<G[cur].size(); i++)
{
Edge &e=edges[G[cur][i]];
if(vis[e.to]||e.cap<=e.flow)continue;
find(e.to);
}
}
int solve()
{
memset(vis,,sizeof(vis));
for(int i=; i<G[s].size(); i++)
{
Edge &e=edges[G[s][i]];
if(vis[e.to]||e.cap<=e.flow)continue;
find(e.to);
}
int ans=;
for(int i=; i<=n-; i++)
ans+=vis[i];
return ans;
}
}T;
int P[maxn];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
LL S1=,S2=;
T.init(n+);
for(int i=; i<=n; i++)
{
scanf("%d",&P[i]);
if(P[i]>){
S1+=P[i];S2+=P[i];
T.addEdge(n+,i,P[i]);
}else{
S1+=-P[i];
T.addEdge(i,n+,-P[i]);
}
}
for(int i=; i<=m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
T.addEdge(a,b,S1);
}
LL a1=T.Maxflow(n+,n+);
LL a2=T.solve();
printf("%I64d %I64d\n",a2,S2-a1);
}
return ;
}
poj2987 求最大权闭合回路的更多相关文章
- poj2987 Firing 最大权闭合子图 边权有正有负
/** 题目:poj2987 Firing 最大权闭合子图 边权有正有负 链接:http://poj.org/problem?id=2987 题意:由于金融危机,公司要裁员,如果裁了员工x,那么x的下 ...
- hdu3879 最大权闭合回路
题意: 有n个基站可以建立,然后m个团体会使用这些基站进行工作,地i个团体会适应Ai Bi 这两个基站, 如果建成收益Ci, 第j个基站花费Pj,求如何建立使得收益最大, 将每个团体看以一个点,然后 ...
- POJ2987 Firing 最大权闭合图
详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...
- hdu 4293 dp求最大权值不重合区间
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4293 #include<cstdio> #include<cstring> # ...
- Prim算法求最大权,POJ(2485)
题目链接:http://poj.org/problem?id=2485 解题报告: 这里有一点要注意的是,第一个点时,dis数组还没有初始化,还全部为inf.第一次来到更新权时,才把邻接矩阵的数据存到 ...
- Poj1163 The Triangle(动态规划求最大权值的路径)
一.Description 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure 1 shows a number triangle. Write a pro ...
- 紫书 例题 11-5 UVa 10048 (Floyd求最大权值最小的路径)
这道题是Floyd的变形 改成d[i][j] = min(d[i][j], max(d[i][k], d[k][j]))就好了. #include<cstdio> #include< ...
- [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...
- HD2255奔小康赚大钱(最大权匹配模板)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
随机推荐
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- Monkey简介及环境搭建(1)
简介:Monkey是Android SDK自带的测试工具,是一个命令行工具,可以运行在模拟器中或者实际设备中,它向系统发送伪随机的用户事件流(如按键输入,触摸屏输入,手势输入等),实现对正在开发的应用 ...
- linux下nodejs的安装
一.下载 https://nodejs.org/en/download/ 然后,解压 二.配置环境变量 配置环境变量:在/etc/profile文件新增:export NODE_HOME=/usr/l ...
- JavaScript学习笔记--语法
代码风格: 每句结尾不用加分号: 单行注释用//,多行注释用/*...需要注释掉的代码....*/ 严格区分大小写 优雅的代码需要注意锁进 基础知识: Number:JavaScript不区分整数和浮 ...
- awk数组
对于awk '!a[$3]++',需要了解3个知识点 1.awk数组知识,不说了2.awk的基本命令格式 awk 'pattern{action}' 省略action时,默认action是{print ...
- ios多播委托
在现实中回调的需求也分两种 一对一的回调. 一对多的回调. 对于一对一的回调,在IOS中使用delegate.block都能实现.而一对多的回调基本就是通知中心了. 假如现在有一个需求,我们以图片下载 ...
- 使用shell脚本监控用户登陆服务器并发送提示信息给微信
1.需要在/etc/ssh/目录下面创建一个名为sshrc的文件,执行权限可给可不给,那么在有人通过ssh远程登录这台服务器的时候,这段脚本就会被执行 #!/bin/bash ###V1---### ...
- 细细探究MySQL Group Replicaiton — 配置维护故障处理全集(转)
如果转载,请注明博文来源: www.cnblogs.com/xinysu/ ,版权归 博客园 苏家小萝卜 所有.望各位支持!
- Win版:Adobe 全系列软件模拟授权注册破解工具 AMT Emulator V0.9.2
http://www.lookae.com/amtemulator-092/ 下载地址 https://files.cnblogs.com/files/simadi/AMT0.9.rar 支持软件:W ...
- vue-3.0创建项目
.npm install --global @vue/cli .npm install -g @vue/cli-init .vue init webpack my-project