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

【题解】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. RabbitMQ 安装,配置

    1:安装 yum install -y rabbitmq-server   2:主要程序介绍 # 管理插件的程序 /usr/sbin/rabbitmq-plugins # 服务程序 /usr/sbin ...

  2. 软件工程个人作业3——集大通APP案例分析

    第一部分:调研, 评测 1.第一次上手体验 主要界面截图: 感受: 1.界面不美观: 2.特色功能展现模块不突出,以上截图为打开APP所看到的界面展示,但是这些功能都不是该APP的特色功能,显得有些累 ...

  3. 【第三周】每周psp

    代码累计 300+575+475=1250 随笔字数 1700+3000+3785=8485 知识点 Cppunit框架 Ui设计 Scrum站立会议 燃尽图(好像没燃起来) 博客写作技能 本周主要是 ...

  4. javascript之彻底理解prototype

    prototype很简单, 就是提供一种引用的机制. let BaseObject = { toString(){ return this.str; }, }; let Object1 = {str: ...

  5. php面试必知必会常见问题

    1 说出常用的10个数组方法 我觉得数组比较最能体现PHP基础语法的一个数据结构了,下面给大家列一下常用的10个关于操作数组的函数 in_array(判断数组中是否有某个元素) implode(将数组 ...

  6. 九度-题目1203:IP地址

    http://ac.jobdu.com/problem.php?pid=1203 题目描述: 输入一个ip地址串,判断是否合法. 输入: 输入的第一行包括一个整数n(1<=n<=500), ...

  7. bzoj4484[JSOI2015]最小表示

    题意 给出一张DAG,要求删除尽量多的边使得连通性不变.(即:若删边前u到v有路径,则删边后仍有路径).点数30000,边数100000. 分析 如果从u到v有(u,v)这条边,且从u到v只有这一条路 ...

  8. C++解析(7):函数重载分析

    0.目录 1.重载的概念 2.C++中的函数重载 3.函数默认参数遇上函数重载 4.编译器调用重载函数的准则 5.重载与指针 6.C++和C相互调用 7.小结 1.重载的概念 自然语言中的上下文--你 ...

  9. Remember the Word UVALive - 3942(dp+trie)

    题意: 给S个不同的单词和一个长字符串 问将其分解为若干个单词有多少种方法(单词可重复使用) 解析: dp[i]表示在这个字符串中以某个位置i为起点的 的一段子字符串 则这个子字符串若存在某个前缀恰好 ...

  10. 【BZOJ4912】天才黑客(最短路,虚树)

    [BZOJ4912]天才黑客(最短路,虚树) 题面 BZOJ 洛谷 题解 \(Anson\)爷讲过的题目,然而我还是不会做 只有照着\(zsy\)的程序打我才会做....果然太弱了. 这道题目显然是把 ...