[CF1082G]Petya and Graph:最小割
分析
发这篇博客的目的就是要让你们知道博主到底有多菜。
类似于[NOI2006]最大获利。(明明就是一模一样好吧!)
不知道怎么了,半秒就想到用网络流,却没想出怎么建图。
连这么简单的题都没做出来,我实在是太菜了。
反思反思!
简述一下建图方式:
把原图中的边看作点。
S向每条边对应的的点连边,容量为边权。
每条边对应的点向这条边连接的两个点连边,容量为\(1e18\)。
每个原图中的点向T连边,容量为点权。
答案即为总边权-最小割。
代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
#define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
using std::cin;
using std::cout;
using std::endl;
typedef long long LL;
inline int read(){
int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x;
}
const int MAXN=1005;
int n,m,S,T;
int ecnt=1,head[MAXN<<1];
int a[MAXN];
int dep[MAXN<<1],cur[MAXN<<1];
LL maxflow,mincut;
std::queue<int> q;
struct Edge{
int to,nxt;
LL cap;
}e[MAXN<<3];
inline void add_edge(int bg,int ed,LL ca){
ecnt++;
e[ecnt].to=ed;
e[ecnt].nxt=head[bg];
e[ecnt].cap=ca;
head[bg]=ecnt;
}
inline bool bfs(){
memset(dep,0,sizeof dep);
while(!q.empty()) q.pop();
rin(i,1,T) cur[i]=head[i];
dep[S]=1;
q.push(S);
while(!q.empty()){
int x=q.front();
q.pop();
trav(i,x){
int ver=e[i].to;
if(!dep[ver]&&e[i].cap){
dep[ver]=dep[x]+1;
q.push(ver);
}
}
}
return dep[T]>0;
}
LL dfs(int x,LL pref){
if(x==T||!pref) return pref;
LL flow=0,temp;
for(int &i=cur[x];i;i=e[i].nxt){
int ver=e[i].to;
if(dep[ver]!=dep[x]+1) continue;
if(!(temp=dfs(ver,std::min(pref,e[i].cap)))) continue;
pref-=temp;
flow+=temp;
e[i].cap-=temp;
e[i^1].cap+=temp;
if(!pref) return flow;
}
return flow;
}
inline void dinic(){
while(bfs()) maxflow+=dfs(S,1e18);
}
int main(){
n=read(),m=read();
S=n+m+1,T=S+1;
rin(i,1,n){
a[i]=read();
add_edge(m+i,T,a[i]);
add_edge(T,m+i,0);
}
LL ans=0;
rin(i,1,m){
int u=read(),v=read();
LL w=read();
add_edge(S,i,w);
add_edge(i,S,0);
add_edge(i,m+u,1e18);
add_edge(m+u,i,0);
add_edge(i,m+v,1e18);
add_edge(m+v,i,0);
ans+=w;
}
dinic();
mincut=maxflow;
ans-=mincut;
printf("%I64d\n",ans);
return 0;
}
[CF1082G]Petya and Graph:最小割的更多相关文章
- Petya and Graph(最小割,最大权闭合子图)
Petya and Graph http://codeforces.com/contest/1082/problem/G time limit per test 2 seconds memory li ...
- CodeForces1082G Petya and Graph 最小割
网络流裸题 \(s\)向点连边\((s, i, a[i])\) 给每个边建一个点 边\((u, v, w)\)抽象成\((u, E, inf)\)和\((v, E, inf)\)以及边\((E, t, ...
- CF1082G Petya and Graph(最小割,最大权闭合子图)
QWQ嘤嘤嘤 感觉是最水的一道\(G\)题了 顺便记录一下第一次在考场上做出来G qwqqq 题目大意就是说: 给你n个点,m条边,让你选出来一些边,最大化边权减点权 \(n\le 1000\) QW ...
- 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),容量为正无穷大 则该 ...
- CF1082G Petya and Graph
题意 定义图权 = 图中边权总和 - 图中点权总和(空图的图权=0),求 n 个点 m 条边的无向图最大权子图. 把边看成点,这个点与两个原图中的点连边.直接最小割求最大闭合子图即可.
- 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:G. Petya and Graph(裸的最大闭合权图)
Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of n n vertic ...
随机推荐
- python 并发编程 多进程 模拟抢票
抢票是并发执行 多个进程可以访问同一个文件 多个进程共享同一文件,我们可以把文件当数据库,用多个进程模拟多个人执行抢票任务 db.txt {"count": 1} 并发运行,效率高 ...
- Java基础语法—数据输入
我们可以通过 Scanner 类来获取用户的输入.使用步骤如下: 1.导包.Scanner 类在java.util包下,所以需要将该类导入.导包的语句需要定义在类的上面. import java.ut ...
- [Web 前端] 028 jQuery 事件
目录 jQuery 的事件 1. 事件绑定 1.1 事件的获取 1.2 基本绑定 1.3 动态绑定 2. 事件触发 2.1 触发的写法 2.2 常用的鼠标事件 3. 事件冒泡和默认行为 3.1 事件冒 ...
- go net库
1 使用Listen函数创建一个server ln, err := net.Listen("tcp", ":8080") if err != nil { // ...
- [转帖]TLS握手:回顾1.2、迎接1.3
TLS握手:回顾1.2.迎接1.3 novsec2019-05-10共26541人围观 ,发现 2 个不明物体网络安全 *本文原创作者:novsec,本文属于FreeBuf原创奖励计划,未经许可禁止转 ...
- 一些DP上的奇奇怪怪的东西
单调队列&单调栈: 有手就行.jpg 四边形不等式: 若\(w(i,j)\)满足\(\forall a\le b<c\le d,w(a,c)+w(b,d)\le w(b,c)+w(a,d ...
- 小白学Python——Matplotlib 学习(3) 函数图形
import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1,1,50) y = 2*x + 1 plt.figure() ...
- LINUX 必知必会检测表--通读无关语言
一.linux和os: 1.命令:netstat tcpdump ipcs ipcrm 这四个命令的熟练掌握程度基本上能体现实际开发和调试程序的经验 2.cpu 内存 硬盘 等等与系统性能调试相关的命 ...
- CABasicAnimation animationWithKeyPath Types
转自:http://www.cnblogs.com/pengyingh/articles/2379631.html CABasicAnimation animationWithKeyPath 一些规定 ...
- django基础篇01-环境的搭建和项目的创建
本文参考自银角大王的博客 基本配置 常用命令: django-admin startproject xxx(项目名) python3 manage.py startapp xxx(app名) pyth ...