[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 ...
随机推荐
- 【Linux U-boot】U-Boot相关命令
<1> help --帮助命令环境变量相关: <2> printenv --查看环境变量也可以使用缩写:print 查看某一个环境变量的值,例子:printenv bootar ...
- 使用ntpdate 同步 linux的时间
1. linux 查看时间和时区的命令 timedatectl 效果为: Local time: Sun -- :: CST Universal time: Sun -- :: UTC RTC tim ...
- Java数据结构之单向环形链表(解决Josephu约瑟夫环问题)
1.Josephu(约瑟夫.约瑟夫环)问题: 设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m ...
- vue组件化编程应用
写几个小案例来理解vue的组件化编程思想,下面是一个demo. 效果图示: 功能: Add组件用于添加用户评论,提交后右边评论回复会立马显示数据.Item组件点击删除可以删除当前用户评论.当List组 ...
- Redis集群,备份,哨兵机制
原文:https://blog.csdn.net/zy345293721/article/details/87536144 1.集群 先来简单了解下redis中提供的集群策略, 虽然re ...
- 初学Python写二进制文件
初学Python写二进制文件 把一个图片的16进制数据保存到一个txt文本,从这个txt文本读出并保存为二进制文件jpg图片文件.说明:图片读出的0xff粘贴ff到文本中,读出时是字符串的”ff”. ...
- 计算机系统结构总结_Multiprocessor & cache coherence
Textbook:<计算机组成与设计——硬件/软件接口> HI<计算机体系结构——量化研究方法> QR 最后一节来看看如何实现parallelism 在多处 ...
- 攻防世界--IgniteMe
测试文件:https://adworld.xctf.org.cn/media/task/attachments/fac4d1290e604fdfacbbe06fd1a5ca39.exe 1.准备 获取 ...
- Vue 变量,成员,属性监听
Vue变量 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF ...
- author认证模块
author认证模块 用auth模块 你就用全套 不是自己写一部分 用别人一部分 创建超级管理员,用于登录DJango admin的后台管理 命令:createsuperuser,输入顺序用户 ...