题目链接:https://www.luogu.org/problemnew/show/P3410

这个题就是求一个最大权闭合图

在一个图中,一些点构成一个集合,且集合中的出边指向的终点也在这个集合中,则我们称这个集合为闭合图。

整个图中点的权值之和最大的闭合图,为最大权闭合图。

最大权闭合图可以用网络流来求

造出一个超级源点S和一个超级汇点T,把S连边到所有带有正权的点上,容量是这个点的权;把所有带负权的点连边到T,容量是这个点的权的相反数。原来的边呢,把它们的容量都设成无限大。

(带负权的是员工需要花费的,正权是我们的收入,收入-花费为所求)

之后我们跑一遍最大流。

答案就是图中的所有正权值之和减去最小割容量。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6 + 10;
const int inf = 1e9;
int n, m, s, t, deep[maxn], maxflow, w[maxn], ans;
struct edge{
int next, to, flow;
}e[maxn<<2];
int head[maxn], cnt = -1;
queue<int> q;
void add(int u, int v, int w)
{
e[++cnt].flow = w; e[cnt].next = head[u]; e[cnt].to = v; head[u] = cnt;
e[++cnt].flow = 0; e[cnt].next = head[v]; e[cnt].to = u; head[v] = cnt;
}
bool bfs(int s, int t)
{
memset(deep, 0x7f, sizeof(deep));
while(!q.empty()) q.pop();
q.push(s); deep[s] = 0;
while(!q.empty())
{
int now = q.front(); q.pop();
for(int i = head[now]; i != -1; i = e[i].next)
{
if(deep[e[i].to] > inf && e[i].flow)
{
deep[e[i].to] = deep[now] + 1;
q.push(e[i].to);
}
}
}
if(deep[t] < inf) return true;
else return false;
}
int dfs(int now, int t, int limit)
{
if(!limit || now == t) return limit;
int flow = 0, f;
for(int i = head[now]; i != -1; i = e[i].next)
{
if(deep[e[i].to] == deep[now] + 1 && (f = dfs(e[i].to, t, min(e[i].flow, limit))))
{
flow += f;
limit -= f;
e[i].flow -= f;
e[i^1].flow += f;
if(!limit) break;
}
}
return flow;
}
void Dinic(int s, int t)
{
while(bfs(s, t))
maxflow += dfs(s, t, inf);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d%d",&m,&n);
s = 1, t = 2 + n + m;
for(int i = 1; i <= m; i++)
{
int u;
scanf("%d",&w[i]);
ans += w[i];
while(scanf("%d",&u) && u != 0)
{
add(i + n + 1, u + 1, inf);
}
}
for(int i = 1; i <= n; i++)
{
int c;
scanf("%d",&c);
add(i + 1, t, c);
}
for(int i = 1; i <= m; i++)
{
add(s, i + n + 1, w[i]);
}
Dinic(s, t);
printf("%d",ans - maxflow);
return 0;
}

最新文章

  1. iOS--NSDate的基本操作和常用核心功能
  2. centos7安装jdk8
  3. 解决Debian系统的Crontab执行时间时差问题
  4. VC++时间函数总结
  5. ASP.NET MVC ActionResult的实现
  6. Unity3D NGUI自适应屏幕分辨率(2014/4/17更新)
  7. ubuntu常用命令操作
  8. Compass 更智能的搜索引擎(2)--进阶
  9. js&#183;逻辑运算
  10. 「Android」GreenDao
  11. springboot+ELK+logback日志分析系统demo
  12. Spark学习笔记——键值对操作
  13. leetcode-algorithms-15 3Sum
  14. LeetCode题解之Binary Tree Postorder Traversal
  15. shell脚本技巧记录
  16. 002杰信-陌生的maven-web项目整改成我们熟悉的Web架构;classpath的含义;ssm框架的整合;junit测试
  17. ssh 端口转发实现外网 80 端口映射到内网 80 端口
  18. 如何区别cookie和token?---测试cookie和token接口时先看。
  19. [Leetcode Week16]Range Sum Query - Mutable
  20. 剑指Offer——替换空格

热门文章

  1. zookeeper 编程框架 curator
  2. TOJ 3486 Divisibility
  3. 自己用 python 实现 base64 编码
  4. [转]Create Custom Exception Filter in ASP.NET Core
  5. SQL中CASE 的用法 转载
  6. 初学Hadoop之计算TF-IDF值
  7. DJango小总结一
  8. js之方法
  9. 移动端点击a链接出现蓝色背景问题解决
  10. ArcGIS图框工具5.2发布,支持ArcGIS10.0,10.110.2,支持国家2000坐标系