loj117 有源汇有上下界最小流
code:
#include<bits/stdc++.h>
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define ll long long
#define inf 1000000001
#define y1 y1___
using namespace std;
char gc(){
static char buf[],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
ll read(){
char ch=gc();ll x=;int op=;
for (;!isdigit(ch);ch=gc()) if (ch=='-') op=-;
for (;isdigit(ch);ch=gc()) x=(x<<)+(x<<)+ch-'';
return x*op;
}
#define N 60005
#define M 130005+N<<1
int n,m,cnt=,s_,t_,s,t,head[N],d[N],vis[N],cur[N];
struct edge{int to,nxt,c;}e[M];
void adde(int x,int y,int c){
e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;
e[cnt].c=c;
}
void ins(int x,int y,int z){
adde(x,y,z);adde(y,x,);
}
bool bfs(){
queue<int> q;q.push(s);
rep (i,,t) vis[i]=-;vis[s]=;
while (!q.empty()){
int u=q.front();q.pop();
for (int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if (e[i].c&&vis[v]==-) vis[v]=vis[u]+,q.push(v);
}
}
return vis[t]!=-;
}
int dfs(int u,int flow){
if (u==t) return flow;
int w,used=;
for (int &i=cur[u];i;i=e[i].nxt){
int v=e[i].to;
if (e[i].c&&vis[v]==vis[u]+){
w=dfs(v,min(flow-used,e[i].c));
e[i].c-=w,e[i^].c+=w,used+=w;
if (used==flow) return used;
}
}
if (!used) vis[u]=-;
return used;
}
int dinic(){
int ret=;
while (bfs()){
memcpy(cur,head,sizeof(cur));//当前弧优化
ret+=dfs(s,inf);
}
return ret;
}
int main(){
n=read(),m=read(),s_=read(),t_=read();
rep (i,,m){
int x=read(),y=read(),a=read(),b=read();
ins(x,y,b-a);d[x]-=a,d[y]+=a;
}
s=n+,t=n+;
rep (i,,n) if (d[i]>) ins(s,i,d[i]);else ins(i,t,-d[i]);
dinic();//不加边直接跑,尽可能跑完
ins(t_,s_,inf);
dinic();//加边以后再跑
for (int i=head[s];i;i=e[i].nxt) if (vis[e[i].to]!=-) return puts("please go home to sleep"),;//判无解
printf("%d\n",e[cnt].c);
return ;
}
loj117 有源汇有上下界最小流的更多相关文章
- LOJ117 有源汇有上下界最小流(上下界网络流)
跑出可行流后从原来的汇点向原来的源点跑最大流,原图最小流=inf-maxflow.显然超源超汇的相关边对其也没有影响.原图最小流=可行流-原图新增流量,因为t向s流量增加相当于s向t流量减少.但为什么 ...
- loj #117. 有源汇有上下界最小流
题目链接 有源汇有上下界最小流,->上下界网络流 注意细节,边数组也要算上后加到SS,TT边. #include<cstdio> #include<algorithm> ...
- LOJ.117.[模板]有源汇有上下界最小流(Dinic)
题目链接 有源汇有上下界最小流 Sol1. 首先和无源汇网络流一样建图,求SS->TT最大流: 然后连边(T->S,[0,INF]),再求一遍SS->TT最大流,答案为新添加边的流量 ...
- sgu 176 Flow construction(有源汇的上下界最小流)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11025 [模型] 有源汇点的上下界最小流.即既满足上下界又满足 ...
- Flow construction SGU - 176 有源汇有上下界最小流 二分法和回流法
/** 题目:Flow construction SGU - 176 链接:https://vjudge.net/problem/SGU-176 题意: 有源汇有上下界的最小流. 给定n个点,m个管道 ...
- 【 POJ - 3801】Crazy Circuits(有源汇、上下界最小流)
Description You’ve just built a circuit board for your new robot, and now you need to power it. Your ...
- bzoj 2502 清理雪道(有源汇的上下界最小流)
[题意] 有一个DAG,要求每条边必须经过一次,求最少经过次数. [思路] 有上下界的最小流. 边的下界为1,上界为无穷.构造可行流模型,先不加ts边跑一遍最大流,然后加上t->s的inf边跑 ...
- sgu176 Flow Construction【有源汇有上下界最小流】
同样是模板题. 首先将有源汇转换为无源汇,假设原来的源汇为st,我们加入的源汇为ST,那么我们应该从t到s连一条流量为+∞的边,使原来的st满足收支平衡,退化为普通节点. 分离必要边和其他边,从S到T ...
- Crazy Circuits HDU - 3157(有源汇有上下界最小流)
给出每条边的下界 求最小流 板题 提供两个板子代码 虽然这个题 第一个比较快 但在loj上https://loj.ac/problem/117 的板题 第一个1700+ms 第二个才600+ms ...
随机推荐
- [POJ2356]Find a multiple 题解(鸽巢原理)
[POJ2356]Find a multiple Description -The input contains N natural (i.e. positive integer) numbers ( ...
- mybatis笔记之使用Mapper接口注解
1. mybatis支持的映射方式 mybatis支持的映射方式有基于xml的mapper.xml文件.基于java的使用Mapper接口class,简单学习一下mybatis使用接口来配置映射的方法 ...
- redis中插入用户集合的语句,有四个属性
一.redis 数据结构使用场景 原来看过 redisbook 这本书,对 redis 的基本功能都已经熟悉了,从上周开始看 redis 的源码.目前目标是吃透 redis 的数据结构.我们都知道,在 ...
- password passphrase passcode 的区别
In general, passphrases are long passwords and passcodes are numeric-only passwords.
- 338.Counting Bits---位运算---《剑指offer》32
题目链接:https://leetcode.com/problems/counting-bits/description/ 题目大意:求解从0到num的所有数的二进制表示中所有1的个数. 法一:暴力解 ...
- Django Authentication 用户认证系统
一. Django的认证系统 Django自带一个用户认证系统,用于处理用户账户.群组.许可和基于cookie的用户会话. 1.1 概览 Django的认证系统包含了身份验证和权限管理两部分.简单地说 ...
- HDU 2544 最短路(floyd+bellman-ford+spfa+dijkstra队列优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找点1到点n的最短路(无向图) 练一下最短路... dijkstra+队列优化: #i ...
- python生成器 协程
生成器 参考文章: 协程 gevent 生成器进阶 看个例子: def gg(): n='' i=0 while True: n=yield i #通过send传入到n if not n: pa ...
- spring boot 使用不同的profile来加载不同的配置文件
在开发过程之中,经常需要在开发和测试环境中进行互相切换,当切换的同时需要加载相应的配置文件,因此要经常 性的对配置文件进行相应的修改,长此以往感到十分痛苦.如果能针对开发和测试环境分别建两个不同的配置 ...
- Oracle中生成uuid的方法
Oracle中生成uuid的方法 下载LOFTER客户端 在Oracle SQL 提供了一个生成uuid的函数sys_guid: http://download.oracle.com/docs/cd/ ...