ZOJ 2314 Reactor Cooling(无源汇上下界网络流)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314
题意:
给出每条边流量的上下界,问是否存在可行流,如果存在则输出。
思路:
先定义D(u)为顶点u发出的所有弧的流量下界与进入顶点u的所有弧的流量下界和之差(out【u】-in【u】)。
对于无源汇的网络流来说:
(1)新增两个顶点S(附加源点)和T(附加汇点)。
(2)对原网络中每个顶点u,计算出D(u),如果D(u)>0,则增加一条新弧<u,T>,这条弧的容量为D(u);如果D(u)<0,则增加一条新弧<S,u>,这条弧的容量为-D(u);如果D(u)=0,则不增加弧。
(3)原网络中的每条弧的容量更改为c-b(上界-下界)。
跑一遍最大流,如果满流,则存在可行流,每条边的实际流量=每条边的流量+该边流量下界。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; int n, m; int in[maxn];
int out[maxn];
int b[maxn]; struct Edge
{
int from,to,cap,flow;
Edge(int u,int v,int w,int f):from(u),to(v),cap(w),flow(f){}
}; struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int cur[maxn];
int d[maxn]; void init(int n)
{
this->n=n;
for(int i=;i<n;++i) G[i].clear();
edges.clear();
} void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,) );
edges.push_back( Edge(to,from,,) );
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
} bool BFS()
{
queue<int> Q;
memset(vis,,sizeof(vis));
vis[s]=true;
d[s]=;
Q.push(s);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=;i<G[x].size();++i)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
} int DFS(int x,int a)
{
if(x==t || a==) return a;
int flow=, f;
for(int &i=cur[x];i<G[x].size();++i)
{
Edge &e=edges[G[x][i]];
if(d[e.to]==d[x]+ && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>)
{
e.flow +=f;
edges[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 num)
{
for(int i=;i<num;i++)
{
printf("%d\n",edges[*i].flow+b[i]);
}
}
}DC; int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n, &m);
int src=, dst=n+;
DC.init(dst+); memset(in,,sizeof(in));
memset(out,,sizeof(out)); for(int i=;i<m;i++)
{
int u, v, c;
scanf("%d%d%d%d",&u,&v,&b[i],&c);
DC.AddEdge(u,v,c-b[i]);
out[u]+=b[i];
in[v]+=b[i];
} int flow=;
for(int i=;i<=n;i++)
{
if(out[i]<in[i])
{
DC.AddEdge(src,i,in[i]-out[i]);
flow+=in[i]-out[i];
}
else
{
DC.AddEdge(i,dst,out[i]-in[i]);
}
} if(DC.Maxflow(src,dst)!=flow) {puts("NO");puts("");continue;} puts("YES");
DC.print(m);
puts("");
}
return ;
}
ZOJ 2314 Reactor Cooling(无源汇上下界网络流)的更多相关文章
- ZOJ 2314 Reactor Cooling [无源汇上下界网络流]
贴个板子 #include <iostream> #include <cstdio> #include <cstring> #include <algorit ...
- ZOJ 2314 - Reactor Cooling - [无源汇上下界可行流]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 The terrorist group leaded by ...
- ZOJ2314 Reactor Cooling(无源汇上下界可行流)
The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...
- ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...
- ZOJ 2314 Reactor Cooling | 无源汇可行流
题目: 无源汇可行流例题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题解: 证明什么的就算了,下面给出一种建图方式 ...
- HDU 4940 Destroy Transportation system(无源汇上下界网络流)
Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...
- hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )
题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...
- POJ 2396 Budget(有源汇上下界网络流)
Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...
- zoj 2314 Reactor Cooling (无源汇上下界可行流)
Reactor Coolinghttp://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 Time Limit: 5 Seconds ...
随机推荐
- Testlink在CentOS、windows安装
有幸在CentOS\windows上都安装过Teslink程序,总结一下.如下: 一.CentOS安装: 1.安装包需要: xampp xampp-linux-x64-5.6.3-0-installe ...
- 【Android】Android背景选择器selector用法汇总
一.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片 <?xml version="1.0" encoding="utf-8&quo ...
- IntelliJ Idea 2018 注册码
转自:http://idea.lanyus.com/ *.lanyus.com及*.qinxi1992.cn下的全部授权服务器已遭JetBrains封杀 请搭建自己的IntelliJ IDEA授权服务 ...
- ThinkPHP做自动登陆及异位或加密COOKIE!
异位或加密方法: /* *登陆如果自动登陆加密 *默认是0解密状态,1是加密 *采用的方法是异位或加密 */ function encrytion($value,$type=0){ $key = md ...
- C语言模拟ATM机界面
虽然是满屏的printf.printf.printf.printf......尴尬 但是一个小项目做下来还是能学习到很多的,有很多小的问题,不是亲自来敲一遍代码,是不会发现的.他的框架,每一个小函数功 ...
- 整数快速幂hdu(1852)
hdu1852 Beijing 2008 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others ...
- html<input>输入框中各种正则表达式设置
<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding ...
- CSS Spritec下载,精灵图,雪碧图,初探之原理、使用
CSS Spritec下载,精灵图,雪碧图,初探之原理.使用 关于CSS Sprite CSSSprites在国内很多人叫css精灵雪碧图,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零 ...
- 设计模式之——Chain of Responsibility
Chain of Responsibility模式又叫做责任链模式,是将多个对象组成一条职责链,然后按照职责链上的顺序一个一个的找出是谁来负责处理. 这个模式很简单,下面就是一个实例程序,有六个处理器 ...
- Air Raid---hdu1151(最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1151 最小路径覆盖 == 顶点数 - 最大匹配. #include<stdio.h> #i ...