「NOI2006」最大获利

传送门

最小割。

对于每一组用户群 \(A_i, B_i, C_i\) ,连边 $S \to A_i, S \to B_i, $ 容量为成本,还有 \(i \to T\) ,容量为收益 \(C_i\),\(A_i, B_i\) 都向 \(i\) 连边,容量为 \(inf\) ,割掉与 \(S\) 的连边表示支付成本,割掉与 \(T\) 的连边表示放弃收益。

参考代码:

#include <algorithm>
#include <cstring>
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
} const int _ = 55010, __ = 155010, INF = 2147483647; int tot = 1, head[_];
struct Edge { int ver, cap, nxt; } edge[__ << 1];
inline void Add_edge(int u, int v, int d) { edge[++tot] = (Edge) { v, d, head[u] }, head[u] = tot ; }
inline void link(int u, int v, int d) { Add_edge(u, v, d), Add_edge(v, u, 0); } int n, m, s, t, dep[_], cur[_], Q[_]; inline int bfs() {
int hd = 0, tl = 0;
memset(dep, 0, sizeof (int) * (t - s + 1));
Q[++tl] = s, dep[s] = 1;
while (hd < tl) {
int u = Q[++hd];
for (rg int i = head[u]; i; i = edge[i].nxt) {
int v = edge[i].ver;
if (dep[v] == 0 && edge[i].cap > 0)
dep[v] = dep[u] + 1, Q[++tl] = v;
}
}
return dep[t] > 0;
} inline int dfs(int u, int flow) {
if (u == t) return flow;
for (rg int& i = cur[u]; i; i = edge[i].nxt) {
int v = edge[i].ver;
if (dep[v] == dep[u] + 1 && edge[i].cap > 0) {
int res = dfs(v, min(flow, edge[i].cap));
if (res) { edge[i].cap -= res, edge[i ^ 1].cap += res; return res; }
}
}
return 0;
} inline int Dinic() {
int res = 0;
while (bfs()) {
for (rg int i = s; i <= t; ++i) cur[i] = head[i];
while (int d = dfs(s, INF)) res += d;
}
return res;
} int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
int sum = 0;
read(n), read(m), s = 0, t = n + m + 1;
for (rg int x, i = 1; i <= n; ++i) read(x), link(s, i, x);
for (rg int x, y, z, i = 1; i <= m; ++i)
read(x), read(y), read(z), link(x, i + n, INF), link(y, i + n, INF), link(i + n, t, z), sum += z;
printf("%d\n", sum - Dinic());
return 0;
}

「NOI2006」最大获利的更多相关文章

  1. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  2. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  3. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  4. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  5. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  6. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  7. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

  8. 「2014-3-17」C pointer again …

    记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

  9. 「2014-3-13」Javascript Engine, Java VM, Python interpreter, PyPy – a glance

    提要: url anchor (ajax) => javascript engine (1~4 articles) => java VM vs. python interpreter =& ...

随机推荐

  1. PHP 超全局变量之$GLOBALS

    $GLOBALS——引用全局作用域中可用的全部变量. $GLOBALS一个包含了全部变量的全局组合数组.变量的名字就是数组的键.(即所有出现过的全局变量,都可通过$GLOBALS获取到) 注释: “S ...

  2. IntelliJ IDEA 2017.3尚硅谷-----主题

    http://www.riaway.com/

  3. numpy (1.16.4) has the default value of allow_pickle as False.

    My version of numpy (1.16.4) has the default value of allow_pickle as False. numpy版本是1.16.4,allow_pi ...

  4. android底部导航栏实现

    第一种用radiobutton实现 https://wizardforcel.gitbooks.io/w3school-android/content/75.html 布局文件,使用radiogrou ...

  5. docker 报错 docker: Error response from daemon: driver failed....iptables failed:

    现象: [root@localhost test]# docker run --name postgres1 -e POSTGRES_PASSWORD=password -p : -d postgre ...

  6. Codeforces Round #622 (Div. 2)C2

    题意 N长度为500000以内,一个数字两边的数字不能都比他高,最多高一边 求他最大sum.叙述有问题,直接看样例 3 10 6 8 因为6左右都比他高,选择10 6 6或者6 6  8,sum明显前 ...

  7. 2019冬季PAT甲级第三题

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ]; ]; int main(){ ...

  8. java 并交集运算

    在面试的过程中,忘记了List中还可以进行交并集运算,这也是常见的数据问题啊,这也是常见的数据结构问题---集合,面试的过程中一直没有想到这种数据结构 java中API中已经集成了并交集的运算. 代码 ...

  9. iso15693芯片读写工具套件 icode-slix2读写 nfc type 5 tag读写

    iso15693芯片读写工具套件 icode-slix2读写 nfc type 5 tag读写校验套件 iso15693工具套件支持icode-slix,icode-slix2芯片的读写,支持iso1 ...

  10. Java开发中模拟接口工具moco的使用

    场景 在开发中需要依赖一些接口,比如需要请求一个返回Json数据的接口,但是返回Json数据的接口要么是没搭建,要么是交互比较复杂. 此时,就可以使用moco来模拟接口返回接口数据,以便开发和测试工作 ...