这道题求一个图的最大权闭合子图

在国集队论文里面有这种题目的求法

对于图中原有的有向边,连一条流量为INF的边

建立源点s,向所有点权为正数的点连一条流量为点权的边

建立汇点t,向所有点权为负数的点连一条流量为点权相反数的边

然后求这个图的最小割,也就是最大流,就是最大权闭合子图的权值和

最后从源点s搜一下,除了s,其他能走到的点就是最大权闭合子图的节点

详细论证见《最小割模型在信息学竞赛中的应用》胡伯涛

注意:这道题要用long long

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <utility>
#include <map>
#include <stack>
#include <set>
#include <vector>
#include <queue>
#include <deque>
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define LL long long
#define Pair pair<int,int>
#define LOWBIT(x) x & (-x)
using namespace std;const int MOD=1e9+7;
const LL INF=1e16;
const int magic=348;int t,tot=1,head[10048],to[200048],nxt[200048];LL f[200048];
inline void addedge(int s,int t,LL cap)
{to[++tot]=t;nxt[tot]=head[s];head[s]=tot;f[tot]=cap;to[++tot]=s;nxt[tot]=head[t];head[t]=tot;f[tot]=0;
}int n,e,cnt=0;
int a[10048];
bool visited[10048];int depth[10048];queue<int> q;
bool bfs()
{int i,x,y;for (i=0;i<=t;i++) depth[i]=-1;depth[0]=0;q.push(0);while (!q.empty()){x=q.front();q.pop();for (i=head[x];i;i=nxt[i]){y=to[i];if (f[i] && depth[y]==-1){depth[y]=depth[x]+1;q.push(y);}}}//for (i=0;i<=t;i++) cout<<depth[i]<<' ';//for (i=head[4];i;i=nxt[i]) cout<<to[i]<<' '<<f[i]<<endl;//cout<<endl;if (depth[t]==-1) return false; else return true;
}LL dfs(int x,LL maxf)
{if (x==t) return maxf;int i,y;LL minf,now,ans=0;for (i=head[x];i;i=nxt[i]){y=to[i];if (f[i] && depth[y]==depth[x]+1){//if (x==4 && y==3) cout<<"*"<<endl;minf=min(f[i],maxf-ans);now=dfs(y,minf);f[i]-=now;f[i^1]+=now;ans+=now;}}return ans;
}void Dfs(int x)
{visited[x]=true;cnt++;for (int i=head[x];i;i=nxt[i])if (f[i] && !visited[to[i]]) Dfs(to[i]);
}int main ()
{int i,x,y;LL sum=0;scanf("%d%d",&n,&e);for (i=1;i<=n;i++) scanf("%d",&a[i]);for (i=1;i<=e;i++){scanf("%d%d",&x,&y);addedge(x,y,INF);}t=n+1;for (i=1;i<=n;i++) {if (a[i]>0) {addedge(0,i,a[i]);sum+=a[i];}if (a[i]<0) addedge(i,t,-a[i]);}LL ans=0;while (bfs()) ans+=dfs(0,INF);//cout<<ans<<endl;Dfs(0);cout<<cnt-1<<' '<<sum-ans<<endl;return 0;
}

POJ2987: Firing 题解相关推荐

  1. POJ2987 Firing

    Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 11037   Accepted: 3335 Descript ...

  2. POJ2987 Firing 最大权闭合图

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

  3. POJ2987 Firing (网络流)

    题意分析 最大权闭合和回路 代码总览 #include<cstdio> #include<bitset> #include<cstring> #include< ...

  4. Firing(POJ-2987)

    Problem Description You've finally got mad at "the world's most stupid" employees of yours ...

  5. POJ 2987 Firing(最大权闭合图)

    [题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...

  6. poj 2987 Firing (最大权闭合子图)

    Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10248   Accepted: 3110 Descript ...

  7. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  8. [JS][dp]题解 | #打家劫舍(一)#

    题解 | #打家劫舍(一)# 题目链接 打家劫舍(一) 题目描述 描述 你是一个经验丰富的小偷,准备偷沿街的一排房间,每个房间都存有一定的现金,为了防止被发现,你不能偷相邻的两家,即,如果偷了第一家, ...

  9. [JS]题解 | #魔法数字#

    题解 | #魔法数字# 题目链接 魔法数字 题目描述 牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的. 操作共有三种,如下: 在当前数字的基础上 ...

最新文章

  1. bazel编译tensorflow 生成libtensorflow_inference.so 和 libandroid_tensorflow_inference_java.jar
  2. excel调用python编程-如何在excel中调用python脚本
  3. iOS开发-63】Unknown type name CGRect,did you mean Rect?的解决方案
  4. 基于注解的SpringMVC整合JPA
  5. linux怎样反向查找路径,Linux中find的用法
  6. php mysql会员注册_PHP实现会员注册系统
  7. 树莓派python编程小车_树莓派智能车AlphaBot教程11:Python 网络编程
  8. 基础电路设计知识:电阻、电容、电感、二极管、三极管、mos管!
  9. MAC中安装Navicat Premium
  10. PAL电视制式知识点
  11. 计算机 取得高级权限,win10获取system权限,win7获取最高权限
  12. 三菱FX3U源码在V10.5的基础上增加了禁止上传功能
  13. 【工作记录】VRLe工作总结
  14. 让独居的父母,时刻受到保护,老人远程无线监护解决方案
  15. 使用wireshark抓取聊天信息与爬虫入门
  16. 字符串解压缩类库(zip、GZIP、QuickLz、snappy、lzf、jzlib)介绍
  17. 万字长文Python面试题,建议先收藏
  18. Nginx 简易教程
  19. 【HAN】代码逐句理解三
  20. http与https协议是什么

热门文章

  1. 慕课 北大曹健《人工智能实践-Tensorflow2.0》 全套讲义ppt 和代码数据集
  2. SAP 查询系统日志- 追踪用户的更改记录
  3. Tensorflow+SSD+Yolo(目标检测)文章14:爬虫
  4. 【C++】初窥C++
  5. Web--Css样式大全
  6. 【JS逆向百题斩】百度翻译接口逆向
  7. 关于黎曼几何初学者的踩坑经历
  8. linux下Gaussian16安装
  9. 计算机开机先检测什么问题,电脑按下开机键后 没反应 但检测电源却没有問題,是什么情况?...
  10. Mudbox与ZBrush有什么区别?