loj #117. 有源汇有上下界最小流
有源汇有上下界最小流,->上下界网络流
注意细节,边数组也要算上后加到SS,TT边。
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<iostream> using namespace std; const int N = ;
const int INF = 1e9; struct Edge{
int to,nxt,c;
}e[];
int head[N],dis[N],cur[N],M[N];
int q[],L,R;
int tot = ,n,m,S,T,Sum; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2)?EOF:*p1++;
}
inline int read() {
int x = ,f = ;char ch = nc();
for (; ch<''||ch>'';ch=nc()) if(ch=='-') f=-;
for (; ch>=''&&ch<=''; ch=nc()) x=x*+ch-'';
return x * f;
}
void add_edge(int u,int v,int c) {
e[++tot].to = v,e[tot].c = c,e[tot].nxt = head[u],head[u] = tot;
}
bool bfs() {
for (int i=; i<=n+; ++i)
cur[i] = head[i],dis[i] = -;
L = ,R = ;
q[++R] = S;
dis[S] = ;
while (L <= R) {
int u = q[L++];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (dis[v] == - && e[i].c > ) {
dis[v] = dis[u] + ;
q[++R] = v;
if (v == T) return true;
}
}
}
return false;
}
int dfs(int u,int flow) {
if (u == T) return flow;
int used = ;
for (int &i=cur[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (dis[v] == dis[u] + && e[i].c > ) {
int tmp = dfs(v,min(e[i].c,flow-used));
if (tmp > ) {
e[i].c -= tmp;e[i^].c += tmp;
used += tmp;
if (used == flow) break;
}
}
}
if (used != flow) dis[u] = -;
return used;
}
int dinic(int s,int t) {
S = s,T = t;
int ans = ;
while (bfs()) ans += dfs(S,INF);
return ans;
}
int main () {
n = read(),m = read();
int s = read(),t = read();
int ss = n + ,tt = n + ;
for (int i=; i<=m; ++i) {
int u = read(),v = read(),b = read(),c = read();
add_edge(u,v,c-b);
add_edge(v,u,);
M[u] -= b;M[v] += b;
}
for (int i=; i<=n; ++i) {
if (M[i] > ) add_edge(ss,i,M[i]),add_edge(i,ss,),Sum += M[i];
if (M[i] < ) add_edge(i,tt,-M[i]),add_edge(tt,i,);
}
add_edge(t,s,INF); //-
add_edge(s,t,);
if (dinic(ss,tt) != Sum) {
printf("please go home to sleep");
return ;
}
int ans = e[tot].c;
e[tot].c = ;e[tot ^ ].c = ;
ans -= dinic(t,s);
printf("%d",ans);
return ;
}
loj #117. 有源汇有上下界最小流的更多相关文章
- 2018.08.20 loj#117. 有源汇有上下界最小流(模板)
传送门 这题真有意思... 先是有一个点T的我怀疑人生. 然后学大佬们封装了我的dinic就莫名其妙的过了??? 所以说锅给谁好呢? 给dinic吧... 解法就是先求出一段可行流,然后从t到s加一条 ...
- 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 [模型] 有源汇点的上下界最小流.即既满足上下界又满足 ...
- loj #116. 有源汇有上下界最大流
题目链接 有源汇有上下界最大流,->上下界网络流 注意细节,重置cur和dis数组时,有n+2个点 #include<cstdio> #include<algorithm> ...
- Flow construction SGU - 176 有源汇有上下界最小流 二分法和回流法
/** 题目:Flow construction SGU - 176 链接:https://vjudge.net/problem/SGU-176 题意: 有源汇有上下界的最小流. 给定n个点,m个管道 ...
- Loj#116-[模板]有源汇有上下界最大流
正题 题目链接:https://loj.ac/p/116 题目大意 \(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 ...
- Crazy Circuits HDU - 3157(有源汇有上下界最小流)
给出每条边的下界 求最小流 板题 提供两个板子代码 虽然这个题 第一个比较快 但在loj上https://loj.ac/problem/117 的板题 第一个1700+ms 第二个才600+ms ...
- bzoj 2502 清理雪道(有源汇的上下界最小流)
[题意] 有一个DAG,要求每条边必须经过一次,求最少经过次数. [思路] 有上下界的最小流. 边的下界为1,上界为无穷.构造可行流模型,先不加ts边跑一遍最大流,然后加上t->s的inf边跑 ...
随机推荐
- (原创)odoo关系字段在视图中的行为控制 总结
字段类型 选项或属性 格式示例 描述 many2one , many2many_tags(widget) no_create options='{"no_create":True} ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(七)一次线上Mysql数据库崩溃事故的记录
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 文章简介 工作这几年,技术栈在不断更新,项目管理心得也增加了不少,写 ...
- TRIO-basic指令--FLEXLINK
Type: Axis Command Syntax: FLEXLINK(base_dist, excite_dist, link_dist, base_in, base_out, excite_acc ...
- 分布式监控系统Zabbix--完整安装记录 -添加apache监控
前面介绍了zabbix3.0.3环境及相关监控项的添加,下面介绍下针对apache的监控配置:1)在apache配置文件中打开server-status状态访问功能(自带的) [root@IDC-Ad ...
- Redis数据"丢失"讨论及规避和解决的几点总结
Redis大部分应用场景是纯缓存服务,请求后端有Primary Storage的组件,如MySQL,HBase;请求Redis的键未命中,会从primary Storage中获取数据返回,同时更新Re ...
- Scrum Meeting 5
第五次会议 No_00:工作情况 No_01:任务说明 待完成 已完成 No_10:燃尽图 No_11:照片记录 待更新 No_100:代码/文档签入记录 No_101:出席表 ...
- Linux内核总结博客 20135332武西垚
http://www.cnblogs.com/wuxiyao/p/5220677.htmlhttp://www.cnblogs.com/wuxiyao/p/5247571.htmlhttp://www ...
- Java源码--Array
1. Arrays.asList() 该方法是将数组转化为List,需要注意以下几点: (1)该方法不适用于基本数据类型(byte,short,int,long,float,double,boolea ...
- 个人项目junit4测试
一.题目简介 用java编写一个程序,模拟ATM柜员机. 二.源码的github链接 www.github.com/liuxianchen/test 三.所设计的模块测试用例.测试结果截图 四 心得 ...
- jsp的自定义标签
1.相对于JSTL或Spring等第三方标签库而言的,用来实现项目中特定的功能需求. 2.自定义标签基本的组成部分 ①页面上看得见的部分 [1]通过taglib引入标签库 [2]标签本身 ②xxx.t ...