ZOJ 2314 Reactor Cooling | 无源汇可行流
题目:
无源汇可行流例题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314
题解:
证明什么的就算了,下面给出一种建图方式
1.建虚拟的S和T
2.每一条原图的边(u,v),设最大容量是Max,最小是Min,建一条容量为Max-Min的边(u,v),同时令ind[v]和oud[u]+=Min,表示实际应该多流入(出)的流量
3.对于每个点u,如果ind[u]>oud[u],为了满足流量平衡条件,所以让S和u连一条容量为ind[u]-oud[u]的边
如果ind[u]<oud[u],同理,我们让u和T连一条容量为oud[u]-ind[u]的边
4.跑最大流
5.如果S的出边都流满就说明有解,反之没有
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#define N 205
#define M 100005
#define INF 0x3f3f3f3f
using namespace std;
int TT,n,m,S,T,ans,sum,dis[N],cur[N],u[M],v[M],mi[M],ma[M],ind[N],oud[N],ecnt=,head[N];
queue<int> q;
struct adj{int nxt,v,w;}e[M];
int read()
{
int ret=,neg=;char j=getchar();
for (;j>'' || j<'';j=getchar())
if (j=='-') neg=-;
for (;j>='' && j<='';j=getchar())
ret=ret*+j-'';
return ret*neg;
}
void add(int u,int v,int w)
{
e[++ecnt].v=v;e[ecnt].w=w;e[ecnt].nxt=head[u];head[u]=ecnt;
e[++ecnt].v=u;e[ecnt].w=;e[ecnt].nxt=head[v];head[v]=ecnt;
}
void init()
{
ans=sum=;ecnt=;
for (int i=;i<=T;i++)
ind[i]=oud[i]=head[i]=;
}
bool Bfs()
{
while (!q.empty()) q.pop();
for (int i=;i<=T;i++)
cur[i]=head[i],dis[i]=-;
dis[S]=;q.push(S);
while (!q.empty())
{
int u=q.front();q.pop();
for (int i=head[u],v;i;i=e[i].nxt)
if (e[i].w && dis[v=e[i].v]==-)
{
dis[v]=dis[u]+,q.push(v);
if (v==T) return ;
}
}
return ;
}
int Dfs(int u,int flow)
{
if (u==T) return flow;
int ret=,delta;
for (int &i=cur[u],v;i;i=e[i].nxt)
if (e[i].w && dis[v=e[i].v]==dis[u]+)
{
delta=Dfs(v,min(e[i].w,flow-ret));
if (delta)
{
e[i].w-=delta;
e[i^].w+=delta;
ret+=delta;
if (ret==flow) break;
}
}
return ret;
}
int main()
{
TT=read();
while (TT--)
{
n=read();m=read();S=n+;T=n+;
init();
for (int i=;i<=m;i++)
{
u[i]=read();v[i]=read();mi[i]=read();ma[i]=read();
add(u[i],v[i],ma[i]-mi[i]);
oud[u[i]]+=mi[i],ind[v[i]]+=mi[i];
}
for (int i=;i<=n;i++)
if (ind[i]>oud[i])
add(S,i,ind[i]-oud[i]),sum+=ind[i]-oud[i];
else
add(i,T,oud[i]-ind[i]);
while (Bfs())
ans+=Dfs(S,INF);
if (ans<sum)
puts("NO");
else
{
puts("YES");
for (int i=;i<=m;i++)
printf("%d\n",mi[i]+e[i*+].w);
}
}
return ;
}
ZOJ 2314 Reactor Cooling | 无源汇可行流的更多相关文章
- ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...
- ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...
- ZOJ 2314 Reactor Cooling [无源汇上下界网络流]
贴个板子 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...
- ZOJ 1314 Reactor Cooling | 上下界无源汇可行流
ZOJ 1314 Reactor Cooling | 上下界无源汇可行流 题意 有一个网络,每条边有流量的上界和下界,求一种方案,让里面的流可以循环往复地流动起来. 题解 上下界无源汇可行流的模型: ...
- 算法复习——无源汇可行流(zoj2314)
题目: The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nucl ...
- zoj 2314 Reactor Cooling (无源汇上下界可行流)
Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...
- Zoj 2314 Reactor Cooling(无源汇有上下界可行流)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意: 给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...
- ZOJ 2314 (sgu 194) Reactor Cooling (无源汇有上下界最大流)
题意: 给定n个点和m条边, 每条边有流量上下限[b,c], 求是否存在一种流动方法使得每条边流量在范围内, 而且每个点的流入 = 流出 分析: 无源汇有上下界最大流模板, 记录每个点流的 in 和 ...
- ZOJ2314 Reactor Cooling(无源汇上下界可行流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...
随机推荐
- ethereum Pet Shop
在部署宠物商店时遇到的一些坑,给大家总结一下,以免大家多走弯路 转载的地址(详细):https://steemit.com/cn/@lucia3/ethereum-pet-shop 启动"n ...
- 介绍三种PHP加密解密算法
PHP加密解密算法 这里主要介绍三种常用的加密解密算法:方法一: /** * @param $string 要加密/解密的字符串 * @param string $operation 类型,ENCOD ...
- 一个好用的C# HttpCookieHelper.cs类
using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressi ...
- BigData--hadoop集群搭建之hbase安装
之前在hadoop-2.7.3 基础上搭建hbase 详情请见:https://www.cnblogs.com/aronyao/p/hadoop.html 基础条件:先配置完成zookeeper 准备 ...
- 齐博cms最新SQL注入网站漏洞 可远程执行代码提权
齐博cms整站系统,是目前建站系统用的较多的一款CMS系统,开源,免费,第三方扩展化,界面可视化的操作,使用简单,便于新手使用和第二次开发,受到许多站长们的喜欢.开发架构使用的是php语言以及mysq ...
- 3D Food Printing【3D食物打印】
3D Food Printing There's new frontier in 3D printing that's begining to come into focus: food. 3D打印的 ...
- BFS 队列
Plague Inc. is a famous game, which player develop virus to ruin the world. JSZKC wants to model thi ...
- set<pair<int,int> > 的运用
In this cafeteria, the N tables are all ordered in one line, where table number 1 is the closest to ...
- 一种简单实用的双向电平转换电路3.3V-5V
当你使用3.3V的单片机的时候,电平转换就在所难免了,经常会遇到3.3转5V或者5V转3.3V的情况,这里介绍一个简单的电路,他可以实现两个电平的相互转换(注意是相互哦,双向的,不是单向的!).电路十 ...
- 转载:BUG定位
1.web前端 Web前端就是通常说的网页.互联网公司的前端一般包含如下内容:JavaScript.ActionScript.CSS.HTML(..ML).Flash.交互式设计.视觉设计 web前端 ...