题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314

题意:

给n个点,及m根pipe,每根pipe用来流躺液体的,单向的,每时每刻每根pipe流进来的物质要等于流出去的物质,要使得m条pipe组成一个循环体,里面流躺物质。

并且满足每根pipe一定的流量限制,范围为[Li,Ri].即要满足每时刻流进来的不能超过Ri(最大流问题),同时最小不能低于Li。

求的是最大流。

很久之前就看了带上下界的网络流,一直没看懂,以为刘汝佳的书上写错了,哈哈~~~

建图:

修改成普通的网络流,但是要保证流量守恒,那么,ss到v的容量,u到tt的容量为 b,而且,要这些附加的边上的流必须满载。

然后题目要求最大的流,那么要把这个可行流找出来,这样,我建边的时候,先不加ss,tt的边,

这样,可行流就在edge[i*2]的地方。

 #include <bits/stdc++.h>

 using namespace std;

 #define inf 0x3f3f3f3f

 const int maxn = ;

 //int low[maxn],in[maxn],out[maxn];

 struct Edge
{
int from,to,cap,flow;
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edge;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn]; void init()
{
for(int i=;i<maxn;i++)
G[i].clear();
edge.clear();
memset(d,,sizeof(d));
memset(vis,,sizeof(vis));
memset(cur,,sizeof(cur));
} void AddEdge (int from,int to,int cap)
{
edge.push_back((Edge){from,to,cap,});
edge.push_back((Edge){to,from,,});
m = edge.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s] = ;
vis[s] = ;
while(!Q.empty())
{
int x = Q.front();
Q.pop();
for(int i=; i<G[x].size(); i++)
{
Edge & e = edge[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to] = ;
d[e.to] = d[x] + ;
Q.push(e.to);
}
}
}
return vis[t];
} long long DFS(int x,int a)
{
if(x==t||a==) return a;
long long flow = ,f;
for(int & i = cur[x]; i<G[x].size(); i++)
{
Edge & e = edge[G[x][i]];
if(d[x] + ==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>)
{
e.flow +=f;
edge[G[x][i]^].flow -=f;
flow +=f;
a-=f;
if(a==) break;
}
}
return flow;
} int Maxflow (int s,int t) {
this->s = s;this->t = t;
int flow = ;
while(BFS()) {
memset(cur,,sizeof(cur));
flow+=DFS(s,inf);
}
return flow;
} void print(int cnt) {
for(int i=;i<cnt;i++)
printf("%d\n",edge[i*].flow+edge[G[][i]].flow);
} }sol; int main()
{
int t;
scanf("%d",&t);
while(t--) {
int n,m;
scanf("%d%d",&n,&m);
int low[maxn],uu[maxn],vv[maxn];
int sum = ;
sol.init();
for(int i=;i<m;i++) {
int u,v,b,c;
scanf("%d%d%d%d",&u,&v,&b,&c);
low[i] = b;
uu[i] = u;
vv[i] = v;
sol.AddEdge(u,v,c-b);
sum+=b;
// out[u] +=low[i];
// in[v] +=low[i];
// sol.AddEdge(0,v,b);
// sol.AddEdge(u,n+1,b);
} for(int i=;i<m;i++) {
sol.AddEdge(,vv[i],low[i]);
sol.AddEdge(uu[i],n+,low[i]);
} int maxflow = sol.Maxflow(,n+);
if(sum!=maxflow)
puts("NO");
else {
puts("YES");
// for(int i=0;i<sol.G[0].size();i++) {
// printf("%d\n",sol.edge[sol.G[0][i]].flow);
// }
sol.print(m);
}
}
return ;
}

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

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

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

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

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

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

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

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

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

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

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

  6. [ZOJ2341]Reactor Cooling解题报告|带上下界的网络流|无源汇的可行流

    Reactor Cooling The terrorist group leaded by a well known international terrorist Ben Bladen is bul ...

  7. ZOJ 2314 Reactor Cooling

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

  8. zoj 2314 Reactor Cooling 网络流

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

  9. 【zoj2314】Reactor Cooling 有上下界可行流

    题目描述 The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuc ...

随机推荐

  1. Java Compare接口

    在Java集合框架中有两种比较接口: Comparable 接口和 Comparator 接口. 一.Comparable 接口  public interface Comparable<T&g ...

  2. sqlserver 事务日志

    预写式日志(Write-Ahead Logging (WAL)) --在数据写入到数据库之前,先写入到日志. 1.”Begin Tran”记录  -> 缓冲区 2. 日志             ...

  3. my24_mysql索引-使用篇

    索引提示 SELECT * FROM table1 USE INDEX (col1_index,col2_index) ; SELECT * FROM table1 IGNORE INDEX (col ...

  4. T-SQL 聚合函数Count与NULL

    大家都知道聚合函数是做统计用的,而count函数是统计行数的,也就是满足一定条件记录的行数. 下面我们来看下这个count与NULL的微妙关系. CREATE TABLE dbo.Student ( ...

  5. 转 Python 操作 MySQL 数据库

    #########http://www.runoob.com/python/python-mysql.html Python 标准数据库接口为 Python DB-API,Python DB-API为 ...

  6. Sphinx Building Docs in horizon

    Building Contributor Documentation This documentation is written by contributors, for contributors. ...

  7. (转)DB2 8.2 for aix5L安装和配置步骤

    DB2 8.2 for aix5L安装和配置步骤[@more@] AIX5.2上安装DB2 V8.2安装平台:IBM eserver,AIX 5.3 64位 一 删除以有的DB2系统: 1.停止DB2 ...

  8. LeetCode 981.基于时间的键值存储(C++)

    创建一个基于时间的键值存储类 TimeMap,它支持下面两个操作: 1. set(string key, string value, int timestamp) 存储键 key.值 value,以及 ...

  9. 021-动态生成验证码jsp代码模板

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. Nodejs计时器定时执行函数

    一.最low的定时器: 每次执行完间隔5s,然后继续执行 (function schedule() { setTimeout(do_it, 5000, schedule); }()); functio ...