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(无源汇上下界网络流)的更多相关文章

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

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

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

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

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

    The terrorist group leaded by a well known international terrorist Ben Bladen is buliding a nuclear ...

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

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

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

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

  6. HDU 4940 Destroy Transportation system(无源汇上下界网络流)

    Problem Description Tom is a commander, his task is destroying his enemy’s transportation system. Le ...

  7. hdu 4940 Destroy Transportation system( 无源汇上下界网络流的可行流推断 )

    题意:有n个点和m条有向边构成的网络.每条边有两个花费: d:毁坏这条边的花费 b:重建一条双向边的花费 寻找这样两个点集,使得点集s到点集t满足 毁坏全部S到T的路径的费用和 > 毁坏全部T到 ...

  8. POJ 2396 Budget(有源汇上下界网络流)

    Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...

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

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

随机推荐

  1. poj3744 Scout YYF I[概率dp+矩阵优化]

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8598   Accepted: 2521 Descr ...

  2. Android搜索自动提示功能 AutocompleteTextView

    1.配置main.xml中自动提示控件: <AutoCompleteTextView android:id="@+id/autotv_searchresult" androi ...

  3. ExtJS学习

    ExtJS是一门比较纠结的框架,自己不太熟,因为现在在做一些老项目,所以没办法要学点.记录下.其实Ext也不是很难,主要是多查查API,了解其基本的用法,然后慢慢去学习,学成之后做管理系统还是很有优势 ...

  4. PL/SQL常用设置

    tools-->preferences-->user interface-->editor-->AutoReplace AutoReplaceWhen enabled, you ...

  5. 处理URL传递中文乱码问题

    在网上搜了很多资料都没有搞定,一般都有以下几种说法: 方法1:在后台中先获得字符串的iso-8859-1编码形式数组,再使用此数组实例一个UTF-8编码形式String类型字符串. 页面提交的url为 ...

  6. Linux系统下Redis缓存安装配置

    Redis是一个高性能的key-value数据库,现时越来越多企业与应用使用Redis作为缓存服务器.楼主是一枚JAVA后端程序员,也算是半个运维工程师了.在Linux服务器上搭建Redis,怎么可以 ...

  7. HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)

    Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. android.os.Handler

    android.os.handler A Handler allows you to send and process Message and Runnable objects associated ...

  9. 基于linux-2.6.35的class_create(),device_create解析

    基于linux-2.6.35的class_create(),device_create解析 作者:苗老师,华清远见嵌入式学院讲师. 从linux内核2.6的某个版本之后,devfs不复存在,udev成 ...

  10. Spongebob and Squares---cf599D(数学公式1 + (1+2) + (1+2+3) +....)

    题目链接:http://codeforces.com/contest/599/problem/D 一个3×5(m×n)的长方形,里面包含15个边长为1的正方形,有8个边长为2的正方形,有3个边长为3的 ...