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 ...
随机推荐
- RMAN.DBMS_RCVCAT 版本错误处理
登录新装的catalog库准备注册数据库时报版本问题 rman target / catalog rman11g/rman2010#@rman Recovery Manager:Release 11. ...
- LeetCode 944 Delete Columns to Make Sorted 解题报告
题目要求 We are given an array A of N lowercase letter strings, all of the same length. Now, we may choo ...
- I2C驱动框架(kernel-2.6.22.6)
以用i2c通信的实时时钟为例 框架入口源文件:i2c_m41t11.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6 硬件平台:JZ2440 以下是驱动框架 ...
- 28-1-LTDC显示中英文
1.字符编码 由于计算机只能识别 0 和 1,文字也只能以 0 和 1 的形式在计算机里存储,所以我们需要对文字进行编码才能让计算机处理,编码的过程就是规定特定的 01 数字串表示特定的文字,最简单的 ...
- swiper 视频轮番
百度搜索:swiper 视频轮番 转载1:https://blog.csdn.net/Aimee1608/article/details/79637929 项目中使用swiper插件嵌套video标签 ...
- PHP 测试杂项
// 驼峰转下划线 function humpToUnderline($str){ if(empty($str)){ return ""; } $arr = str_split($ ...
- mosquitto SSL认证
[11]MQTT mosquitto 双向SSL认证配置方式 [12]MQTT mosquitto 单向SSL认证的配置方式 Mosquitto服务器的搭建以及SSL/TLS安全通信配置(比较可信) ...
- api-gateway-engine知识点(2)
GroupVersion实现engine本地缓存 package com.inspur.cloud.apigw.engine.cache; import java.util.Map;import ja ...
- Linux个人知识扩展:服务器几u的意思,网络带宽
服务器几u的意思: 指的服务器大小规格 1U=4.45cm 2U=8.9cm 3U=4.45cm * 3 4U=4.45cm * 4 这指的是服务器的高度 现在的服务器为节省空间都是很扁的 U是服务器 ...
- poj3126
被坑了3个小时,本来以为算法错了,谁知道,竟然是素数筛弄错了 !!! #include <iostream>#include <stdio.h>#include <str ...