建图差不多和以前做的差不多,就是最后询问这个闭合子图有多少个的时候,只要输出这个图的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 求最大权闭合回路的更多相关文章

  1. poj2987 Firing 最大权闭合子图 边权有正有负

    /** 题目:poj2987 Firing 最大权闭合子图 边权有正有负 链接:http://poj.org/problem?id=2987 题意:由于金融危机,公司要裁员,如果裁了员工x,那么x的下 ...

  2. hdu3879 最大权闭合回路

    题意: 有n个基站可以建立,然后m个团体会使用这些基站进行工作,地i个团体会适应Ai Bi 这两个基站, 如果建成收益Ci,  第j个基站花费Pj,求如何建立使得收益最大, 将每个团体看以一个点,然后 ...

  3. POJ2987 Firing 最大权闭合图

    详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...

  4. hdu 4293 dp求最大权值不重合区间

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4293 #include<cstdio> #include<cstring> # ...

  5. Prim算法求最大权,POJ(2485)

    题目链接:http://poj.org/problem?id=2485 解题报告: 这里有一点要注意的是,第一个点时,dis数组还没有初始化,还全部为inf.第一次来到更新权时,才把邻接矩阵的数据存到 ...

  6. 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 ...

  7. 紫书 例题 11-5 UVa 10048 (Floyd求最大权值最小的路径)

    这道题是Floyd的变形 改成d[i][j] = min(d[i][j], max(d[i][k], d[k][j]))就好了. #include<cstdio> #include< ...

  8. [BZOJ 1497][NOI 2006]最大获利(最大权闭合子图)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1497 分析: 这是在有向图中的问题,且边依赖于点,有向图中存在点.边之间的依赖关系可以 ...

  9. HD2255奔小康赚大钱(最大权匹配模板)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. 九九乘法表python3写入文件中

    写入文件代码如下: with open("e:\\test01.txt","w+",encoding="utf-8") as wq: for ...

  2. 【PyQt5-Qt Designer】添加图片+鼠标点击

    添加图片+鼠标点击 graphicsView中添加图片 效果图 添加之后左边1处 生成qrc文件  选择文件右键编译-生成图片的16进制文件 课后作业:

  3. Java中String类两种实例化的区别(转)

    原文:http://blog.csdn.net/wangdajiao/article/details/52087302 一.String类的第一种方式 1.直接赋值 例:String str = &q ...

  4. Ext.app.Application

    代表性的ExtJS应用程序,通常是用Ext.container.Viewport创建的经典的单页面应用程序. 一个程序由一个或多个视图(View)组成.视图的行为表现由它对应的视图控制器(Ext.ap ...

  5. 1_02 Vue Slot

    slot 插槽 插槽内容 const component ={ template: ` <div> <slot></slot> </div> ` } n ...

  6. cuda9.0编译caffe报错nvcc fatal : Unsupported gpu architecture 'compute_70'

    Tesla V100 cuda9.0 caffe编译的时候报上述错误,修改方法: CUDA_ARCH := #-gencode arch=compute_20,code=sm_20 \ #-genco ...

  7. OC动画:CAKeyframeAnimation

    // 方法一 用法1​ Value方式 //创建动画对象 CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyP ...

  8. Got timeout reading communication packets解决方法

    Got timeout reading communication packets解决方法 http://www.th7.cn/db/mysql/201702/225243.shtml [Note] ...

  9. RzCheckTree基本使用

    procedure TForm1.Button1Click(Sender: TObject); var i: Integer; begin //循环读取勾选节点代码及内容 //StateIndex 1 ...

  10. ORACLE安装入门篇OEL5.4安装ORACLE11g

    一.安装ORACLE11g软件(11.2.0.0) (一)安装前的包支持 1.检测yum仓库是否已经配置好 yum list all 2.搭建yum仓库 1).挂载所需要的安装光盘 虚拟机挂载光盘: ...