Codeforces 903G Yet Another Maxflow Problem 线段树+网络流
来源:互联网 发布:酷狗直播mac版 编辑:程序博客网 时间:2024/05/16 08:41
题意
给出一个图,这个图的点集被分成两部分,设为A和B,其中每部分都有n个点,设为
在
有q次操作,每次操作x y表示将
1<=n,m,q<=200000,1<=c<=10^9
分析
大晚上的打题居然1A了,真的不可思议。
这题首先要想到最大流=最小割,然后只要动态维护最小割即可。
根据这个图的性质,不难发现在割掉
我们可以通过线段树来动态加边并维护删掉B中每条边的代价,取个最小值就是最小割了。
修改的话,随便拿个优先队列或者set记录一下删每条边的值然后瞎搞搞就行了。
代码
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<queue>using namespace std;typedef long long LL;const int N=200005;int n,m,q,cnt,last[N],a[N],b[N];LL val[N];struct Queue{ priority_queue<LL> q1,q2; LL top() { while (!q2.empty()&&q1.top()==q2.top()) q1.pop(),q2.pop(); return q1.top(); } void del(LL x) { q2.push(x); } void push(LL x) { q1.push(x); }}que;struct tree{LL tag,mn;}t[N*5];struct edge{int to,next,w;}e[N];int read(){ int x=0,f=1;char ch=getchar(); while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}void addedge(int u,int v,int w){ e[++cnt].to=v;e[cnt].w=w;e[cnt].next=last[u];last[u]=cnt;}void pushdown(int d,int l,int r){ if (l==r||!t[d].tag) return; LL w=t[d].tag;t[d].tag=0; t[d*2].mn+=w;t[d*2].tag+=w; t[d*2+1].mn+=w;t[d*2+1].tag+=w;}void build(int d,int l,int r){ if (l==r) {t[d].mn=b[l-1];return;} int mid=(l+r)/2; build(d*2,l,mid);build(d*2+1,mid+1,r); t[d].mn=min(t[d*2].mn,t[d*2+1].mn);}void ins(int d,int l,int r,int x,int y,int z){ if (x>y) return; pushdown(d,l,r); if (l==x&&r==y) {t[d].mn+=z;t[d].tag+=z;return;} int mid=(l+r)/2; ins(d*2,l,mid,x,min(y,mid),z); ins(d*2+1,mid+1,r,max(x,mid+1),y,z); t[d].mn=min(t[d*2].mn,t[d*2+1].mn);}int main(){ n=read();m=read();q=read(); for (int i=1;i<n;i++) a[i]=read(),b[i]=read(); for (int i=1;i<=m;i++) { int x=read(),y=read(),z=read(); addedge(x,y,z); } build(1,1,n); for (int i=1;i<=n;i++) { for (int j=last[i];j;j=e[j].next) ins(1,1,n,1,e[j].to,e[j].w); val[i]=t[1].mn;que.push(-val[i]-a[i]); } printf("%I64d\n",-que.top()); while (q--) { int x=read(),y=read(); que.del(-val[x]-a[x]); a[x]=y; que.push(-val[x]-a[x]); printf("%I64d\n",-que.top()); } return 0;}
阅读全文
0 0
- Codeforces 903G Yet Another Maxflow Problem 线段树+网络流
- CF 903G. Yet Another Maxflow Problem
- [codeforces903G]Yet Another Maxflow Problem
- Yet Another Multiple Problem
- Codeforces Gym 100741G Yet Another Median Task 二分乱搞
- hdu4474-Yet Another Multiple Problem
- hdu4474 Yet Another Multiple Problem
- HDU4474 Yet Another Multiple Problem
- Hackerrank:Yet Another KMP Problem
- Yet Another Multiple Problem-----BFS
- Yet Another Number Sequence CodeForces
- Codeforces 868F (Codeforces Round #438 F) Yet Another Minimization Problem DP+分治
- [DP 决策单调 分治]Codeforces 868F .Yet Another Minimization Problem
- Codeforces Round #438:F. Yet Another Minimization Problem(DP决策单调性+二分+莫队)
- Gym 100741G Yet Another Median Task
- spoj 1771 Yet Another N-Queen Problem
- Yet Another Multiple Problem (hdu 4474)
- HDU 4474 Yet Another Multiple Problem
- eclipse 上传代码至git
- 《C++ Primer》读书笔记第十四章-1-重载IO、算术、关系等运算符
- 二叉树的前序中序后序的递归和非递归遍历
- ptmalloc
- javaweb 分页
- Codeforces 903G Yet Another Maxflow Problem 线段树+网络流
- 数据结构实验之排序二:交换排序
- JDK-9的安装与环境变量配置
- 人工智能之自动驾驶系列(一):概要
- LeetCode-21-Merge Two Sorted Lists(C语言实现)
- 串与模式匹配
- 串与模式匹配
- 第一个APP......的Errors
- 山东科技大学-2017年12数据结构期末考试真题