最大权闭合子图

对于这个题,可以抽象成一个图论模型,如果我们把用户与其要求建立的中转站连边,获得的利益看成正权值,付出的代价看成负权值,我们可以发现,选取一个用户的时候,就相当于选取了一个闭合子图。

这里概述一下闭合子图的概念:有向图的闭合图,闭合图内任意点的任意后继也一定还在闭合图中。

所以我们要选的就是整个图的最大权闭合子图。。

对于这种闭合子图模型,通常使用网络流求解。

对于闭合子图的建图方法,大体上是:

  • 建立源点s,与正权点相连,边权为点权

  • 建立汇点t,与负权点相连,边权为点权绝对值

  • 原图中的边照样连,但是为了防止被割掉,所以边权为INF

然后有一个定理:最大权闭合子图的权值=原图正权值之和-最小割

最小割显然不会去割边权为INF的边,这也是为什么要将依赖关系的边边设为INF的原因。

然而我并不知道怎么证明。。也看不懂怎么证明。。。

所以我们跑一遍dinic就可以啦

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 5005;
const int M = 50005;
int n, m, cnt, head[N<<4], depth[N<<4];
struct Edge {int v, next, f; } edge[M<<4]; void addEdge(int a, int b, int f){
edge[cnt].v = b, edge[cnt].f = f, edge[cnt].next = head[a], head[a] = cnt ++;
edge[cnt].v = a, edge[cnt].f = 0, edge[cnt].next = head[b], head[b] = cnt ++;
} bool bfs(){
full(depth, 0);
queue<int> q;
depth[0] = 1;
q.push(0);
while(!q.empty()){
int s = q.front(); q.pop();
for(int i = head[s]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(!depth[u] && edge[i].f > 0){
depth[u] = depth[s] + 1;
q.push(u);
}
}
}
return depth[n + m + 1] != 0;
} int dfs(int s, int a){
if(s == n + m + 1) return a;
int flow = 0;
for(int i = head[s]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(depth[u] == depth[s] + 1 && edge[i].f > 0){
int k = dfs(u, min(a, edge[i].f));
a -= k, flow += k, edge[i].f -= k, edge[i^1].f += k;
}
if(!a) break;
}
if(a) depth[s] = -1;
return flow;
} int dinic(){
int ret = 0;
while(bfs()){
ret += dfs(0, INF);
}
return ret;
} int main(){ full(head, -1);
n = read(), m = read();
for(int i = 1; i <= n; i ++){
int w = read();
addEdge(i + m, n + m + 1, w);
}
int sum = 0;
for(int i = 1; i <= m; i ++){
int u = read(), v = read(), c = read();
addEdge(0, i, c), addEdge(i, u + m, INF), addEdge(i, v + m, INF);
sum += c;
}
printf("%d\n", sum - dinic());
return 0;
}

BZOJ 1497 最大获利的更多相关文章

  1. BZOJ 1497 最大获利(最大权闭合图)

    1497: [NOI2006]最大获利 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 4686  Solved: 2295 [Submit][Statu ...

  2. bzoj 1497 最大获利 - 最小割

    新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前夜,需要做太多的准备工作,仅就站址选择一项,就需要完成前期市场研 ...

  3. BZOJ 1497 最大获利(最大权闭合子图)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 思路:由题意可以得知,每个顾客都依赖2个中转站,那么让中转站连有向边到汇点,流量为它的建设费用 ...

  4. HDU 3879 && BZOJ 1497:Base Station && 最大获利 (最大权闭合图)

    http://acm.hdu.edu.cn/showproblem.php?pid=3879 http://www.lydsy.com/JudgeOnline/problem.php?id=1497 ...

  5. BZOJ 1497: [NOI2006]最大获利 最小割

    1497: [NOI2006]最大获利 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=1497 Description 新的技术正冲击着手 ...

  6. BZOJ 1497: [NOI2006]最大获利( 最大流 )

    下午到周六早上是期末考试...但是我还是坚守在机房....要挂的节奏啊.... 这道题就是网络流 , 建图后就最大流跑啊跑啊跑... --------------------------------- ...

  7. BZOJ 1497: [NOI2006]最大获利(最大权闭合子图)

    1497: [NOI2006]最大获利 Time Limit: 5 Sec  Memory Limit: 64 MB Description 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机 ...

  8. BZOJ 1497 JZYZOJ 1344 [NOI2006]最大获利 网络流 最大权闭合图

    http://www.lydsy.com/JudgeOnline/problem.php?id=1497 http://172.20.6.3/Problem_Show.asp?id=1344   思路 ...

  9. BZOJ 1497: [NOI2006]最大获利

    1497: [NOI2006]最大获利 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 4572  Solved: 2239[Submit][Status] ...

随机推荐

  1. flink1.7 checkpoint源码分析

    初始化state类 //org.apache.flink.streaming.runtime.tasks.StreamTask#initializeState initializeState(); p ...

  2. sublime插件不能使用,提示plugin_host has exited unexpectedly

    sublime Text3一打开软件就提示plugin_host has exited unexpectedly,插件不能使用 解决方法很简单: 1.首先,ctrl + shift + p  --&g ...

  3. python四:函数练习--小白博客

    为什么要有函数?函数式编程定义一次,多出调用函数在一定程度上可以理解为变量函数的内存地址加上()就是调用函数本身也可以当做参数去传参 不用函数:组织结构不清晰代码的重复性 def test():#te ...

  4. c++继承实例

    #include <iostream> #include <vector> #include <string> using namespace std; class ...

  5. Linux安装Apache常见报错(一)

    启动Apache提示报错:Could not reliably determine the server's fully qualified domain name, using localhost, ...

  6. 【问题解决方案】之 jmeter启动报错:Not able to find Java executable or version. Please check your Java installation

    故事发生在云计算实验课上-- ** 故事发生在云计算实验课上-- Step 1 在Xshell中登录自己的cloud虚拟机后,<sudo su ->切换到root用户 Step 2 < ...

  7. openstack,docker,mesos,k8s关系

    openstack,docker,mesos,k8s什么关系? - 知乎https://www.zhihu.com/question/62985699 OpenStack + K8S 环境集成测试ht ...

  8. js-XMLHttpRequest 2级

    ###1. XMLHttpRquest 2级 1)   FormData 现代web应用中频繁使用的一项功能就死表单数据的序列化, XMLHttpRquest 2级为此定义了FormData类型 Fo ...

  9. [转帖]前端-chromeF12 谷歌开发者工具详解 Sources篇

    前端-chromeF12 谷歌开发者工具详解 Sources篇 原贴地址:https://blog.csdn.net/qq_39892932/article/details/82498748 cons ...

  10. 1363. ZigZag Conversion

    public class Solution { /** * @param s: the given string * @param numRows: the number of rows * @ret ...