CodeForces1082G Petya and Graph 最小割

网络流裸题
\(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 最小割的更多相关文章
- Petya and Graph(最小割,最大权闭合子图)
Petya and Graph http://codeforces.com/contest/1082/problem/G time limit per test 2 seconds memory li ...
- poj2125Destroying The Graph(最小割+输出方案)
题目请戳这里 题目大意:给一张有向图,现在要选择一些点,删掉图中的所有边.具体操作为:选择点i,可以选择删除从i出发的所有有向边或者进入i的所有有向边,分别有个代价ini和outi,求最小的代价删掉所 ...
- poj 2125 Destroying The Graph 最小割+方案输出
构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), 连边 (a1,b2),容量为正无穷大 则该 ...
- POJ 2125 Destroying The Graph [最小割 打印方案]
Destroying The Graph Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8311 Accepted: 2 ...
- Petya and Graph/最大权闭合子图、最小割
原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...
- [CF1082G]Petya and Graph:最小割
分析 发这篇博客的目的就是要让你们知道博主到底有多菜. 类似于[NOI2006]最大获利.(明明就是一模一样好吧!) 不知道怎么了,半秒就想到用网络流,却没想出怎么建图. 连这么简单的题都没做出来,我 ...
- CF1082G Petya and Graph(最小割,最大权闭合子图)
QWQ嘤嘤嘤 感觉是最水的一道\(G\)题了 顺便记录一下第一次在考场上做出来G qwqqq 题目大意就是说: 给你n个点,m条边,让你选出来一些边,最大化边权减点权 \(n\le 1000\) QW ...
- POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)
Destroying The Graph Time Limit: 2000MS Memo ...
- Destroying The Graph 最小点权集--最小割--最大流
Destroying The Graph 构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), ...
随机推荐
- BOOST 之filesystem, path
目录[-] 使用 boost::filesystem 的第一个程序 清单 1. 用于确定某个文件的类型是否为 Directory 的代码 了解 Boost path 对象 清单 2. 创建 Boost ...
- UNIX网络编程 第6章 I/O复用:select和poll函数
UNIX网络编程 第6章 I/O复用:select和poll函数
- Spring4笔记1--Spring概述、IoC
Spring概述: Spring框架: Spring 由 20 多个模块组成,它们可以分为数据访问/集成(Data Access/Integration).Web.面向切面编程(AOP, Aspec ...
- margin-bottom无效问题以及div里内容动态居中样式!
最近调前端样式时候,遇到一个需求,在中间文字不对等的情况下想让下面的操作文字距离底部对齐,如图: , 刚开始觉得使用margin-bottom就可以,后来发现只有margin-top是管用的,查了资料 ...
- 禅道CMS 获文件名脚本
use Net::HTTP::GET; use Base64; ; windowWidth=; windowHeight=; sid=jg9g2mk5kmru46lmd3g2evoc87>; # ...
- SolrJ API 官方文档最佳实践
以下内容译自Solr Wiki官方文档,版权没有,随意转载. Solrj 是一个访问solr的Java客户端.它提供了一个java接口用于添加更新和查询solr索引.本页面介绍SolrJ最新版本1.4 ...
- 13-6_mysql索引_1_Mysql_Learning_Notes_20180719_13-6
mysql索引_1_Mysql_Learning_Notes 二分查找/折半查找法,binary search 一种在有序数组中查找某一特定元素的搜索算法; 二分查找法的优点是比较少次数,查找速度快, ...
- Python生成器-博文读后感
Windows 10家庭中文版,Python 3.6.4, 上午看过了一篇讲Python生成器的博文: 提高你的Python: 解释‘yield’和‘Generators(生成器)’(英文原文) 这篇 ...
- Windows 10安装uWSGI:不可行、失败了
Windows 10家庭中文版,Python 3.6.4,uwsgi-2.0.17.tar.gz,压缩工具-7-zip 提示:请不要和我一样尝试,浪费时间,去Linux上玩吧! 几个小时的安装经历 昨 ...
- 数据结构之线性表(python版)
数据结构之线性表(python版) 单链表 1.1 定义表节点 # 定义表节点 class LNode(): def __init__(self,elem,next = None): self.el ...