网络流裸题

\(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. PHP在Linux下Apache环境中执行exec,system,passthru等服务器命令函数

    更多内容推荐微信公众号,欢迎关注: 若在服务器中使用php test.php运行exec,system,passthru等命令相关的脚本能成功运行,在web页面却没反应, [可能是服务器端,PHP脚本 ...

  2. Apple Notification Center Service--ANCS【转】

    Apple Notification Center Service 转自:http://studentdeng.github.io/blog/2014/03/22/ancs/ MAR 22ND, 20 ...

  3. oracle11g的冷热备份

    1.冷备份 如果数据库可以正常关闭,而且允许关闭足够长的时间,那么就可以采用冷备份(脱机备份),可以是归档冷备份,也可以是非归档冷备份.其方法是首先关闭数据库,然后备份所有的物理文件,包括数据文件.控 ...

  4. haproxy支持的负载均衡算法详解

    目前haproxy支持的负载均衡算法有如下8种: 1.roundrobin 表示简单的轮询,每个服务器根据权重轮流使用,在服务器的处理时间平均分配的情况下这是最流畅和公平的算法.该算法是动态的,对于实 ...

  5. Python 正则表达式提高

    re模块的高级用法 search re.search(pattern, string[, flags]) ​ 若string中包含pattern子串,则返回Match对象,否则返回None,注意,如果 ...

  6. virtualenv,virtualenvwrapper安装及使用

    1.安装 # 安装: (sudo) pip install virtualenv virtualenvwrapper # centos7下 pip install virtualenv virtual ...

  7. Oracle学习笔记:实现select top N的方法

    由于Oracle不支持select top N语句,所以在Oracle中需要利用order by和rownum的组合来实现select top N的查询. rownum是记录表中数据编号的一个隐藏字段 ...

  8. CSS常见简写规则整理

    外边距(margin) margin-top margin-right margin-bottom margin-left 简写顺序为顺时针方向(上.右.下.左),如:margin: 1px 2px ...

  9. spark java API 实现二次排序

    package com.spark.sort; import java.io.Serializable; import scala.math.Ordered; public class SecondS ...

  10. Sql Server 添加、更新、查询表注释、字段注释相关sql

    /*******************字段添加注释*********************/ if not exists (SELECT C.value AS column_description ...