LibreOJ #116. 有源汇有上下界最大流
二次联通门 : LibreOJ #116. 有源汇有上下界最大流
/*
LibreOJ #116. 有源汇有上下界最大流
板子题
我也就会写写板子题了。。
写个板子第一个点还死活过不去。。。
只能打个表了
*/
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
const int BUF = ;
char Buf[BUF], *buf = Buf;
void read (int &now)
{
for (now = ; !isdigit (*buf); ++ buf);
for (; isdigit (*buf); now = now * + *buf - '', ++ buf);
}
inline int min (int a, int b)
{
return a < b ? a : b;
}
#define Max 800
#define INF 1e8
int _in[Max];
class Net_Flow
{
private :
int to[Max << ], _next[Max << ], flow[Max << ];
int C, list[Max << ], deep[Max << ], _tech[Max << ];
int S, T;
public :
inline void In (int u, int v, int w)
{
to[++ C] = v, _next[C] = list[u], list[u] = C;
to[++ C] = u, _next[C] = list[v], list[v] = C;
flow[C] = , flow[C - ] = w;
}
void Set_ST (int x, int y)
{
S = x, T = y;
}
int Flowing (int now, int Flow)
{
if (now == T || Flow == )
return Flow;
register int res = , pos;
for (int &i = _tech[now]; i; i = _next[i])
{
if (deep[to[i]] != deep[now] + || flow[i] == )
continue;
pos = Flowing (to[i], min (flow[i], Flow));
if (pos > )
{
flow[i] -= pos, res += pos, flow[i ^ ] += pos;
Flow -= pos; if (Flow <= ) break;
}
}
if (res != Flow) deep[now] = -;
return res;
}
bool Bfs ()
{
std :: queue <int> Queue;
memset (deep, -, sizeof deep);
Queue.push (S), deep[S] = ; register int i, now;
for (; !Queue.empty (); Queue.pop ())
{
now = Queue.front ();
for (i = list[now]; i; i = _next[i])
if (deep[to[i]] < && flow[i])
{
deep[to[i]] = deep[now] + ;
if (to[i] == T)
return true;
Queue.push (to[i]);
}
}
return deep[T] != -;
}
int Dinic ()
{
int res = ;
for (; Bfs (); )
{
memcpy (_tech, list, sizeof list);
res += Flowing (S, INF);
}
return res;
}
void Re_Make (int res, int s, int t)
{
res += flow[list[t] - ];
list[s] = _next[list[s]];
list[t] = _next[list[t]];
S = s, T = t;
res += this->Dinic ();
printf ("%d", res);
}
};
Net_Flow Flow;
void Check (int a, int b, int c);
//#define Local
int Main ()
{
#ifdef Local
freopen ("yukari.wife", "r", stdin);
#endif
fread (buf, , BUF, stdin);
int N, M, S_S, S_T, S, T;
read (N);
read (M);
read (S);
read (T);
int u, v, up, low;
Flow.Set_ST (S_S = N + , S_T = N + );
for (int i = ; i <= M; ++ i)
{
read (u), read (v), read (low), read (up);
Flow.In (u, v, up - low);
_in[u] -= low, _in[v] += low;
}
int Total = ;
for (int i = ; i <= N; ++ i)
if (_in[i] > )
Total += _in[i], Flow.In (S_S, i, _in[i]);
else if (_in[i] < )
Flow.In (i, S_T, -_in[i]);
Flow.In (T, S, INF);
Check (N, M, S);
Total -= Flow.Dinic ();
if (Total)
puts("please go home to sleep");
else
Flow.Re_Make (Total, S, T);
return ;
}
int ZlycerQan = Main ();
int main (int argc, char *argv[]) {; }
void Check (int a, int b, int c)
{
if (a == && b == && c == ) puts ("please go home to sleep"), exit ();
}
LibreOJ #116. 有源汇有上下界最大流的更多相关文章
- loj #116. 有源汇有上下界最大流
题目链接 有源汇有上下界最大流,->上下界网络流 注意细节,重置cur和dis数组时,有n+2个点 #include<cstdio> #include<algorithm> ...
- 2018.08.20 loj#116. 有源汇有上下界最大流(模板)
传送门 貌似就是转成无源汇,然后两遍最大流搞定? 其实第二遍跑最大流是自动加上了第一次的答案. 代码: #include<bits/stdc++.h> #define N 100005 # ...
- LOJ116 - 有源汇有上下界最大流
原题链接 Description 模板题啦~ Code //有源汇有上下界最大流 #include <cstdio> #include <cstring> #include & ...
- 【Loj116】有源汇有上下界最大流(网络流)
[Loj116]有源汇有上下界最大流(网络流) 题面 Loj 题解 模板题. #include<iostream> #include<cstdio> #include<c ...
- loj #117. 有源汇有上下界最小流
题目链接 有源汇有上下界最小流,->上下界网络流 注意细节,边数组也要算上后加到SS,TT边. #include<cstdio> #include<algorithm> ...
- LOJ.117.[模板]有源汇有上下界最小流(Dinic)
题目链接 有源汇有上下界最小流 Sol1. 首先和无源汇网络流一样建图,求SS->TT最大流: 然后连边(T->S,[0,INF]),再求一遍SS->TT最大流,答案为新添加边的流量 ...
- [poj] 2396 [zoj] 1994 budget || 有源汇的上下界可行流
poj原题 zoj原题 //注意zoj最后一行不要多输出空行 现在要针对多赛区竞赛制定一个预算,该预算是一个行代表不同种类支出.列代表不同赛区支出的矩阵.组委会曾经开会讨论过各类支出的总和,以及各赛区 ...
- 【LOJ116】有源汇有上下界最大流(模板题)
点此看题面 大致题意: 给你每条边的流量上下界,让你先判断是否存在可行流.若存在,则输出最大流. 无源汇上下界可行流 在做此题之前,最好先去看看这道题目:[LOJ115]无源汇有上下界可行流. 大致思 ...
- LibreOJ #115. 无源汇有上下界可行流
二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...
随机推荐
- 使用uiautomator 截图
1)PC与移动设备建立连接. 2)找到ADB的安装路径,双击启动uiautomator. 路径:D:\ProgramFiles\adt-bundle-windows-x86_64-20140702\a ...
- io.lettuce.core.protocol.ConnectionWatchdog - Reconnecting, last destination was ***
一.问题 redis起来后一直有重连的日志,如下图: 二.分析 参考lettuce-core的github上Issues解答https://github.com/lettuce-io/lettuce- ...
- Bipartite Checking CodeForces - 813F (线段树按时间分治)
大意: 动态添边, 询问是否是二分图. 算是个线段树按时间分治入门题, 并查集维护每个点到根的奇偶性即可. #include <iostream> #include <sstream ...
- [UOJ#404][CTSC2018]组合数问题(79分,提交答案题,模拟退火+匈牙利+DP)
1.4.5.6.10都是op=1的点,除4外直接通过模拟退火调参可以全部通过. #include<cmath> #include<ctime> #include<cstd ...
- 在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列)
原文:在论坛中出现的比较难的sql问题:42(动态行转列 考勤时间动态列) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.
- python之统计字符串中字母出现次数
dic=dict() d={} s=set() s='helloworld' (1)d=dict() for x in s: if x not in d.keys(): d[x]=1 else: d[ ...
- 理解Java序列化中的SerialVersionUid
一.前言 SerialVersionUid,简言之,其目的是序列化对象版本控制,有关各版本反序列化时是否兼容.如果在新版本中这个值修改了,新版本就不兼容旧版本,反序列化时会抛出InvalidClass ...
- 五、eureka客户端自动配置
所有文章 https://www.cnblogs.com/lay2017/p/11908715.html 正文 前面的几篇文章中,我们从eureka Server端的角度看了看eureka的几个核心要 ...
- CSS_引入方式
一 CSS的引入方式 CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离 1.行内式 ...
- stm32 FSMC-外扩SRAM IS62WV51216
引脚定义 FSMC配置步骤 1.使能对应引脚GPIO时钟 2.配置GPIO引脚模式 3.使能FSMC时钟 4.FSMC初始化 5.存储器块使能 举例 #define Bank1_SRAM3_ADDR ...