【算法】有上下界网络流-无源汇(循环流)

【题解】http://www.cnblogs.com/onioncyc/p/6496532.html

//未提交
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=,maxm=,inf=0x3f3f3f3f;
struct edge{int from,v,flow;}e[maxm];
int S,T,TT,n,m,tot=,first[maxn],cur[maxn],in[maxn],d[maxn],q[],lows[maxm];
void insert(int u,int v,int w)
{
tot++;e[tot].v=v;e[tot].flow=w;e[tot].from=first[u];first[u]=tot;
tot++;e[tot].v=u;e[tot].flow=;e[tot].from=first[v];first[v]=tot;
}
bool bfs()
{
memset(d,-,sizeof(d));
int head=,tail=;q[]=S;d[S]=;
while(head!=tail)
{
int x=q[head++];if(head>)head=;
for(int i=first[x];i;i=e[i].from)
if(d[e[i].v]==-&&e[i].flow)
{
d[e[i].v]=d[x]+;
q[tail++]=e[i].v;
if(tail>)tail=;
}
}
return d[T]!=-;
}
int dfs(int x,int a)
{
if(x==T||a==)return a;
int flow=,f;
for(int& i=cur[x];i;i=e[i].from)
if(d[e[i].v]==d[x]+&&(f=dfs(e[i].v,min(a,e[i].flow)))>)
{
e[i].flow-=f;
e[i^].flow+=f;
a-=f;
flow+=f;
if(a==)break;
}
return flow;
}
int main()
{
scanf("%d",&TT);
while(TT--)
{
memset(first,,sizeof(first));
memset(in,,sizeof(in));
tot=;
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d%d",&u,&v,&lows[i],&w);
in[u]-=lows[i];in[v]+=lows[i];
insert(u,v,w-lows[i]);
}
S=,T=n+;
for(int i=;i<=n;i++)
{
if(in[i]>)insert(S,i,in[i]);
if(in[i]<)insert(i,T,-in[i]);
}
while(bfs())
{
for(int i=;i<=n+;i++)cur[i]=first[i];
dfs(S,inf);
}
bool ok=;
for(int i=first[S];i;i=e[i].from)
if(e[i].flow)ok=;
if(ok)
{
printf("YES\n");
for(int i=;i<=m*;i+=)
printf("%d\n",e[i^].flow+lows[i>>]);
}
else printf("NO\n");
}
return ;
}

【有上下界网络流】【ZOJ】2314 Reactor Cooling的更多相关文章

  1. zoj 2314 Reactor Cooling 网络流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 The terrorist group leaded by a ...

  2. ZOJ 2314 Reactor Cooling(无源汇上下界网络流)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题意: 给出每条边流量的上下界,问是否存在可行流,如果存在则输出. ...

  3. ZOJ 2314 Reactor Cooling 带上下界的网络流

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

  4. ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...

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

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

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

    Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...

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

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

  8. ZOJ 2314 Reactor Cooling

    Reactor Cooling Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...

  9. ZOJ 2314 Reactor Cooling [无源汇上下界网络流]

    贴个板子 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...

  10. ZOJ 2314 Reactor Cooling | 无源汇可行流

    题目: 无源汇可行流例题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题解: 证明什么的就算了,下面给出一种建图方式 ...

随机推荐

  1. Spring学习(五)——Spring注解(一)

    ---恢复内容开始--- 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射 ...

  2. MySQL 忘记root密码怎么办

    前言:记住如果忘记root密码,在启动MySQL的时候,跳过查询授权表就ok了. 对于RedHat 6 而言 (1)启动mysqld 进程时,为其使用:--skip-grant-tables --sk ...

  3. iOS界面设计之基础控件的学习 --- UITextField

    学习iOS界面设计也有段时间了,每次写到一些基础控件(如:UILable . UITextField)的时候就深觉应该总结一个函数来实现这些基础控件的属性设置,所以下面就是我对UITextField的 ...

  4. 结对项目——fault,error,failure的程序设计

    一.结对编程内容: 1.不能触发Fault. 2.触发Fault,但是不触发Error. 3.触发Error,但不触发Failure. 二.结对编程人员 1.周宗耀.周浩: 2.结对截图: 三.结对项 ...

  5. Qt多线程-QtConcurrent并行运算高级API

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt多线程-QtConcurrent并行运算高级API     本文地址:http://tec ...

  6. [Google] 看雪论坛: 安卓碎片化的情况

    2018年10月28日早间消息,谷歌方面发布了Android各版本的最新份额数据,截止到10月26日.即便是已经推出3个月了,Android 9 Pie系统的用户数仍旧没有超过0.1%,导致未出现在榜 ...

  7. Vue数据绑定和响应式原理

    Vue数据绑定和响应式原理 当实例化一个Vue构造函数,会执行 Vue 的 init 方法,在 init 方法中主要执行三部分内容,一是初始化环境变量,而是处理 Vue 组件数据,三是解析挂载组件.以 ...

  8. angular 延迟更新方法

    失去焦点后更新: <input ng-model="name" ng-model-options="{updateOn:'blur'}" />{{n ...

  9. HDU 4758——Walk Through Squares——2013 ACM/ICPC Asia Regional Nanjing Online

    与其说这是一次重温AC自动机+dp,倒不如说这是个坑,而且把队友给深坑了. 这个题目都没A得出来,我只觉得我以前的AC自动机的题目都白刷了——深坑啊. 题目的意思是给你两个串,每个串只含有R或者D,要 ...

  10. SPAMS:稀疏建模工具箱

    https://chunqiu.blog.ustc.edu.cn/?p=570 http://spams-devel.gforge.inria.fr/index.html 在一篇显著性检测文章:Sal ...