网络流裸题

\(s\)向点连边\((s, i, a[i])\)

给每个边建一个点

边\((u, v, w)\)抽象成\((u, E, inf)\)和\((v, E, inf)\)以及边\((E, t, w)\)

最小割建模...

然后就没了....复习一下板子吧


#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std; #define ll long long
#define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --) #define gc getchar
inline int read() {
int p = 0, w = 1; char c = gc();
while(c > '9' || c < '0') { if(c == '-') w = -1; c = gc(); }
while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();
return p * w;
} const int sid = 5e4 + 5;
const ll inf = 1e17; int n, m, s, t, ip, cnp = 1;
int a[sid], q[sid], go[sid], num[sid], L[sid];
int cap[sid], nxt[sid], node[sid];
ll res[sid]; inline void addedge(int u, int v, ll w) {
nxt[++ cnp] = cap[u]; cap[u] = cnp; node[cnp] = v; res[cnp] = w;
nxt[++ cnp] = cap[v]; cap[v] = cnp; node[cnp] = u; res[cnp] = 0;
} #define cur node[i]
inline void bfs() {
int fr = 1, to = 0;
L[t] = 1; num[1] ++; q[++ to] = t;
while(fr <= to) {
int o = q[fr ++];
for(int i = cap[o]; i; i = nxt[i])
if(res[i ^ 1] && !L[cur]) {
q[++ to] = cur;
L[cur] = L[o] + 1; num[L[cur]] ++;
}
}
for(int i = 1; i <= t; i ++) go[i] = cap[i];
} inline ll dfs(int o, ll flow) {
ll tt = 0, tmp;
if(o == t) return flow;
for(int &i = go[o]; i; i = nxt[i]) {
if(L[cur] + 1 != L[o]) continue;
tmp = dfs(cur, min(flow, res[i]));
res[i] -= tmp; res[i ^ 1] += tmp;
flow -= tmp; tt += tmp;
if(!flow) return tt;
}
-- num[L[o]]; if(!num[L[o]]) L[s] = t + 1;
L[o] ++; num[L[o]] ++;
go[o] = cap[o];
return tt;
} inline ll isap(int s, int t) {
bfs();
ll ret = dfs(s, inf);
while(L[s] <= t) ret += dfs(s, inf);
return ret;
} int main() {
ll ans = 0;
n = read(); m = read();
rep(i, 1, n) a[i] = read();
s = n + m + 1; t = s + 1;
rep(i, 1, n) addedge(s, i, a[i]);
rep(i, 1, m) {
int u = read(), v = read(), w = read();
addedge(u, n + i, inf); addedge(v, n + i, inf);
addedge(n + i, t, w); ans += w;
}
ans -= isap(s, t);
printf("%lld\n", ans);
return 0;
}

CodeForces1082G Petya and Graph 最小割的更多相关文章

  1. Petya and Graph(最小割,最大权闭合子图)

    Petya and Graph http://codeforces.com/contest/1082/problem/G time limit per test 2 seconds memory li ...

  2. poj2125Destroying The Graph(最小割+输出方案)

    题目请戳这里 题目大意:给一张有向图,现在要选择一些点,删掉图中的所有边.具体操作为:选择点i,可以选择删除从i出发的所有有向边或者进入i的所有有向边,分别有个代价ini和outi,求最小的代价删掉所 ...

  3. poj 2125 Destroying The Graph 最小割+方案输出

    构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), 连边 (a1,b2),容量为正无穷大 则该 ...

  4. POJ 2125 Destroying The Graph [最小割 打印方案]

    Destroying The Graph Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8311   Accepted: 2 ...

  5. Petya and Graph/最大权闭合子图、最小割

    原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...

  6. [CF1082G]Petya and Graph:最小割

    分析 发这篇博客的目的就是要让你们知道博主到底有多菜. 类似于[NOI2006]最大获利.(明明就是一模一样好吧!) 不知道怎么了,半秒就想到用网络流,却没想出怎么建图. 连这么简单的题都没做出来,我 ...

  7. CF1082G Petya and Graph(最小割,最大权闭合子图)

    QWQ嘤嘤嘤 感觉是最水的一道\(G\)题了 顺便记录一下第一次在考场上做出来G qwqqq 题目大意就是说: 给你n个点,m条边,让你选出来一些边,最大化边权减点权 \(n\le 1000\) QW ...

  8. POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)

                                                          Destroying The Graph Time Limit: 2000MS   Memo ...

  9. Destroying The Graph 最小点权集--最小割--最大流

    Destroying The Graph 构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), ...

随机推荐

  1. Java学习笔记——继承、接口、多态

    浮点数的运算需要注意的问题: BigDecimal operand1 = new BigDecimal("1.0"); BigDecimal operand2 = new BigD ...

  2. 【LinuxC】GCC编译C程序,关闭随机基址

    1.编译.链接和运行程序 C代码示例: #include <stdio.h> #include <stdlib.h> int main() { printf("hel ...

  3. 使用dork脚本来查询Google

    使用dork脚本来查询Google 了解Google Hacking数据库的第一步是了解所有典型的Google运算,就像机器级编程工程师必须了解计算机操作代码一样. 这些Google运算是Google ...

  4. 15 Defer, Panic, and Recover

    Defer, Panic, and Recover 4 August 2010 Go has the usual mechanisms for control flow: if, for, switc ...

  5. spring boot jpa 多数据源配置

    在实际项目中往往会使用2个数据源,这个时候就需要做额外的配置了.下面的配置在2.0.1.RELEASE 测试通过 1.配置文件 配置两个数据源 spring.datasource.url=jdbc:m ...

  6. HDU 2476 String painter(区间DP+思维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意:给你字符串A.B,每次操作可以将一段区间刷成任意字符,问最少需要几次操作可以使得字符串 ...

  7. hihoCoder #1184 : 连通性二·边的双连通分量(边的双连通分量模板)

    #1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老 ...

  8. Oracle学习笔记:decode函数

    decode函数主要作用:将查询结果翻译成其他值(即以其他形式变现出来) 使用方法: SELECT DECODE(colunm_name,值1,翻译值1,值2,翻译值2……值n,翻译值n,缺省值) F ...

  9. ES按资源类型统计个数

    一.目标:统计各类型资源的个数,输出详细报表 http://10.10.6.225:9200/dsideal_db/t_resource_info/ _mapping {  "propert ...

  10. servlet 学习笔记(三)

    同一用户的不同页面共享数据有以下四种方法: 1.sendRedirect()跳转 2.session技术 3.隐藏表单提交(form) 4. cookie技术(小甜饼) --------------- ...