二次联通门 : LibreOJ #115. 无源汇有上下界可行流

/*

    LibreOJ #115. 无源汇有上下界可行流

    板子题
我也就会写写板子题了。。 */
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring> 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 _S, _T; int low[Max << ], _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 Clear ()
{
C = , S = _S, T = _T;
} 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 (); )
{
for (int i = S; i <= T; ++ i)
_tech[i] = list[i];
res += Flowing (S, INF);
}
return res;
} void Print (const int &M)
{
printf ("YES\n");
for (int i = ; i <= M; ++ i)
printf ("%d\n", low[i] + flow[i << | ]);
}
};
Net_Flow Flow; //#define Local int Main ()
{ #ifdef Local freopen ("yukari.wife", "r", stdin);
#endif
fread (buf, , BUF, stdin); int N, M;
read (N);
read (M);
int u, v, up;
_S = , _T = N + , Flow.Clear (); for (int i = ; i <= M; ++ i)
{
read (u), read (v), read (low[i]), read (up);
Flow.In (u, v, up - low[i]);
_in[u] -= low[i], _in[v] += low[i];
}
int Total = ;
for (int i = ; i <= N; ++ i)
if (_in[i] > )
Total += _in[i], Flow.In (_S, i, _in[i]);
else if (_in[i] < )
Flow.In (i, _T, -_in[i]); if (Flow.Dinic () != Total)
return printf ("NO"), ;
else Flow.Print (M); return ;
}
int ZlycerQan = Main ();
int main (int argc, char *argv[]) {; }

LibreOJ #115. 无源汇有上下界可行流的更多相关文章

  1. LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)

    #115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...

  2. [loj#115] 无源汇有上下界可行流 网络流

    #115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据   题 ...

  3. 2018.08.20 loj#115. 无源汇有上下界可行流(模板)

    传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...

  4. loj#115. 无源汇有上下界可行流

    \(\color{#0066ff}{ 题目描述 }\) 这是一道模板题. \(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \ ...

  5. 【LOJ115】无源汇有上下界可行流(模板题)

    点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...

  6. Zoj 2314 Reactor Cooling(无源汇有上下界可行流)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意:    给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...

  7. 无源汇有上下界可行流(ZQU 1590)

    无源汇有上下界可行流(也就是循环流) 模型:一个网络,求出一个流,使得每条边的流量必须>=Li且<=Hi, 每个点必须满足总流入量=总流出量(流量守恒)(这个流的特点是循环往复,无始无终) ...

  8. 【模板】无源汇有上下界可行流(网络流)/ZOJ2314

    先导知识 网络最大流 题目链接 https://vjudge.net/problem/ZOJ-2314 题目大意 多组数据,第一行为数据组数 \(T\). 对于每一组数据,第一行为 \(n,m\) 表 ...

  9. ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...

随机推荐

  1. Scala 算法案例

    移除第一个负数之后的所有负数 // 构建数组 val a = ArrayBuffer[Int]() a += (1, 2, 3, 4, 5, -1, -3, -5, -9) // 每发现一个第一个负数 ...

  2. Windows10无法远程连接

    1.在桌面计算机上,选择右键->属性.左侧任务列表中选择远程设置(如果桌面没有计算机图标,按键盘+R 输入 control system) 钩上允许 远程连接到此计算机 正常情况下,这样就可以在 ...

  3. dev 从表处理

    从表列名,从表选中行和主表选中一样,var selectrow = detailView.GetRow(detailView.FocusedRowHandle) as obj; private voi ...

  4. The Art Of Loving

    The Art Of Loving 来源 https://www.zhihu.com/question/23720541 ----------------------------- 茫然的蒲公英 有书 ...

  5. SVN上传本地项目到服务器

    1. 在服务器新建一个文件夹目录: 2. 将新建的目录在本地check out下来: 3. 将自己的项目拷贝到check out下来的文件夹下: 4. 右键点击svnàAdd,选择所有添加: 5. 右 ...

  6. arm9交叉编译工具链

    Arm-linux-gcc: gcc和arm-linux-gcc的头文件并不一样. Eg. Arm-linux-ld:链接器,-T参数是使用链接器脚本. Eg. Arm-linux-readelf:读 ...

  7. centos iptables 数据转发

    iptables -t nat -I PREROUTING -p tcp --dport 3389 -j DNAT --to 38.X25.X.X02 iptables -t nat -I POSTR ...

  8. GitHub开源的10个超棒后台管理面板

    目录1.AdminLTE 2.vue-Element-Admin 3.tabler 4.Gentelella 5.ng2-admin 6.ant-design-pro 7.blur-admin 8.i ...

  9. Flutter——Radio组件、RadioListTile组件(单选按钮组件)

    Radio组件 Radio组件的常用属性: 属性 描述 value 单选的值 onChanged 改变时触发 activeColor 选中的颜色.背景颜色 groupValue 选择组的值 impor ...

  10. Django中过滤的实现

    过滤模块 安装 >: pip install django-filter 注册应用:settings/dev.py INSTALLED_APPS = [ # 列表过滤模块 'django_fil ...