loj115 无源汇有上下界可行流
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 205
#define M 10205+N<<1
int n,m,cnt=,s,t,head[N],d[N],vis[N],cur[N];
struct edge{int to,nxt,d,c;}e[M];
void adde(int x,int y,int d,int c){
e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;
e[cnt].d=d;e[cnt].c=c;//d:原图下界;c:新图容量
}
void ins(int x,int y,int d,int z){
adde(x,y,d,z);adde(y,x,d,);
}
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();
rep (i,,m){
int x=read(),y=read(),a=read(),b=read();
ins(x,y,a,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();
for (int i=head[s];i;i=e[i].nxt) if (vis[e[i].to]!=-){puts("NO");exit();}
puts("YES");
rep (i,,m) printf("%d\n",e[i*+].d+e[i*+].c);
return ;
}
loj115 无源汇有上下界可行流的更多相关文章
- LOJ115 无源汇有上下界可行流(上下界网络流)
假设初始流为每条边的下界.但这样可能流量会不守恒,我们需要在上面加上一个附加流使流量守恒.只要让每个点开始的出/入流量与原初始流相等就可以求出附加流了.那么新建超源S超汇T,令degree[i]表示流 ...
- 【LOJ115】无源汇有上下界可行流(模板题)
点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...
- LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)
#115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...
- 2018.08.20 loj#115. 无源汇有上下界可行流(模板)
传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...
- [loj#115] 无源汇有上下界可行流 网络流
#115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据 题 ...
- loj#115. 无源汇有上下界可行流
\(\color{#0066ff}{ 题目描述 }\) 这是一道模板题. \(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \ ...
- Zoj 2314 Reactor Cooling(无源汇有上下界可行流)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...
- LibreOJ #115. 无源汇有上下界可行流
二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...
- 无源汇有上下界可行流(ZQU 1590)
无源汇有上下界可行流(也就是循环流) 模型:一个网络,求出一个流,使得每条边的流量必须>=Li且<=Hi, 每个点必须满足总流入量=总流出量(流量守恒)(这个流的特点是循环往复,无始无终) ...
随机推荐
- CodeForces - 1015D
There are nn houses in a row. They are numbered from 11 to nn in order from left to right. Initially ...
- MFC CListCtrl 将一个列表的选中项添加到另一个列表
MFC CListCtrl 将一个列表的选中项添加到另一个列表, 用VC6.0实现: 简单记录一下自己的学习历程, 和大家分享,如果对你有用,我很高兴. 1.新建一个基于对话框的工程(Dialog-B ...
- solr笔记之安装部署到tomcat
1. 下载 solr 去官网下载,下载的时候选清华的镜像源,这个页面:https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr/7.1.0/ 在/ ...
- AJAX的前世今生与未来
一.通过三个问答来认识一下Ajax 什么是Ajax? Ajax是Asynchronous Javascript And XML的缩写,它是一种技术. Ajax有什么用? 这一技术能够向服务器请求额外的 ...
- 图片异或(xor)getflag
题目地址:https://files.cnblogs.com/files/nul1/flag_enc.png.tar 这题是源于:网鼎杯minified 经过测试隧道红色最低通道异常.其余均正常.所以 ...
- 使用批处理方式从svn 检出DEMO
Branching in Subversion¶ FROM:https://dev.geogebra.org/trac/wiki/SubversionBranching Some people wan ...
- MySQL指定使用某个索引查询语句
查询语句查询emp_no,所以先查询emp_no的索引使用primary select emp_no,salary from salaries use index(s_f_t) where emp_n ...
- 55.Jump Game---dp
题目链接 题目大意:给一个数组,第i个位置的值表示当前可以往前走的最远距离,求从第一个位置能否顺利走到最后一个位置.例子如下: 法一(借鉴):DP,dp[i]表示从上一个位置走到当前位置时,剩余的可以 ...
- 升级vs17中的cordova-simulate
visual studio 17自带的cordova-simulate有一个bug,动态添加的html代码里面如果带有header,会出现js异常导致后面js程序终止执行,这个问题已经给他们提了iss ...
- SyntaxError: Missing parentheses in call to 'print' 这个错误原因是Python版本问题
问题 print "www.baidu.com" Python2 print ("www.baidu.com") Python3 出 ...