SGU 194 Reactor Cooling (有容量和下界的可行流)

题意:给定上一个有容量和下界的网络,让你求出一组可行解。

析:先建立一个超级源点 s 和汇点 t ,然后在输入时记录到每个结点的下界的和,建边的时候就建立c - b的最后再建立 s 和 t , 在建立时,如果 i 结点的输入的大于输出的,那么就是从 s 建立一条边,否则 i 与 t 建立,然后跑一次最大流,就OK了,注意求出的流量是没有下界,再加上下界的就好了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200 + 50;
const int mod = 1000;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r > 0 && r <= n && c > 0 && c <= m;
}
struct Edge{
  int from, to, cap, flow;
};

struct Dinic{
  int n, m, s, t;
  vector<Edge> edges;
  vector<int> G[maxn];
  bool vis[maxn];
  int d[maxn];
  int cur[maxn];

  void init(int n){
    this-> n = n;
    edges.clear();
    for(int i = 0; i < n; ++i)  G[i].clear();
  }

  void addEdge(int from, int to, LL cap){
    edges.push_back((Edge){from, to, cap, 0});
    edges.push_back((Edge){to, from, 0, 0});
    m = edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
  }

  bool bfs(){
    memset(vis, 0, sizeof vis);
    queue<int> q;
    q.push(s);
    d[s] = 0;
    vis[s] = 1;
    while(!q.empty()){
      int x = q.front();  q.pop();
      for(int i = 0; i < G[x].size(); ++i){
        Edge &e = edges[G[x][i]];
        if(!vis[e.to] && e.cap > e.flow){
          vis[e.to] = 1;
          d[e.to] = d[x] + 1;
          q.push(e.to);
        }
      }
    }
    return vis[t];
  }

  int dfs(int x, int a){
    if(x == t || a == 0)  return a;
    int flow = 0, f;
    for(int &i = cur[x]; i < G[x].size(); ++i){
      Edge &e = edges[G[x][i]];
      if(d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0){
        e.flow += f;
        edges[G[x][i]^1].flow -= f;
        flow += f;
        a -= f;
        if(a == 0)  break;
      }
    }
    return flow;
  }

  int maxFlow(int s, int t){
    this->s = s; this->t = t;
    int flow = 0;
    while(bfs()){
      memset(cur, 0, sizeof cur);
      flow += dfs(s, INF);
    }
    return flow;
  }
};
Dinic dinic;
int in[maxn*maxn], out[maxn*maxn];
int B[maxn*maxn];

int main(){
  scanf("%d %d", &n, &m);
  int s = 0, t = n + 1;
  for(int i = 0; i < m; ++i){
    int u, v, b, c;
    scanf("%d %d %d %d", &u, &v, &b, &c);
    dinic.addEdge(u, v, c - b);
    B[i] = b;
    in[v] += b;  out[u] += b;
  }
  int ans = 0;
  for(int i = 1; i <= n; ++i){
    int c = in[i] - out[i];
    if(c > 0)  dinic.addEdge(s, i, c), ans += c;
    else dinic.addEdge(i, t, -c);
  }
  if(dinic.maxFlow(s, t) != ans){ puts("NO");  return 0; }
  puts("YES");
  for(int i = 0; i < m; ++i)
    printf("%d\n", dinic.edges[i<<1].flow + B[i]);
  return 0;
}

  

时间: 2024-08-07 21:18:32

SGU 194 Reactor Cooling (有容量和下界的可行流)的相关文章

SGU 194 Reactor Cooling 带容量上下限制的网络流

建立虚拟的源点和汇点,因为忽略掉了容量下界之后会导致流量不平衡,所以对于每个u,v,u多出来的流量流到汇点,v不够的流量从源点补流 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <ma

sgu 194 Reactor Cooling(有容量上下界的无源无汇可行流)

[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=20757 [题意] 求有容量上下界的无源无汇可行流. [思路] 无源无汇可行流要求所有的顶点都满足流量平衡. 基本思路是转化成最大流来做. 对于边(u,v,b,c),连边(u,v,c-b).为了保持流量平衡,我们还需要连边         1.(S,u,inB[u]-outB[u])  inB>outB 2.(u,T,outB[u]-inB[u])  outB>

SGU 194. Reactor Cooling(无源汇有上下界的网络流)

时间限制:0.5s 空间限制:6M 题意: 显然就是求一个无源汇有上下界的网络流的可行流的问题 Solution: 没什么好说的,直接判定可行流,输出就好了 code /* 无汇源有上下界的网络流 */ #include <iostream> #include <cstring> #define ms(a,b) memset(a,b,sizeof a) using namespace std; const int MAXN = 209; struct node { int u, v

SGU 194 Reactor Cooling ——网络流

[题目分析] 无源汇上下界可行流. 上下界网络流的问题可以参考这里.↓ http://www.cnblogs.com/kane0526/archive/2013/04/05/3001108.html [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> //#include <map> #include <set> #include

SGU 194 Reactor Cooling Dinic求解 无源无汇有上下界的最大流

题目链接 题意:有向图中有n(1 <= n <= 200)个点,无自环或者环的大小至少为3.给定每条边的最小流量和最大流量,问每条边的可行流量为多少? 思路:一般求解的网络流并不考虑下界问题,即流量可以为0,在有下界时,我们只需将上界变成r-l,这时还需要满足流量守恒,增加源点s和汇点t,当点u的流入大于流出时,将点u与s连边,容量即为多出的流量.同理当u流出大于流入时,多出来的流出的流量连到汇点t;直接跑最大流: 注:这时判断是否存在可行解时,是看连到源点的所有边是否满流,全为满流说明内部流

SGU 194 Reactor Cooling

没有源点和汇点.流量有上下界的网络最大流问题. 今天看了一下午的有上下界的网络最大流问题,在似懂非懂.非常朦胧的状态下过了这个题...激动了半天. 求解的方法:http://www.cnblogs.com/zufezzt/p/4681035.html AC代码: #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<vector> #include

[ACdream 1211 Reactor Cooling]无源无汇有上下界的可行流

题意:无源无汇有上下界的可行流 模型 思路:首先将所有边的容量设为上界减去下界,然后对一个点i,设i的所有入边的下界和为to[i],所有出边的下界和为from[i],令它们的差为dif[i]=to[i]-from[i],根据流量平衡原理,让出边和入边的下界相抵消,如果dif[i]>0,说明入边把出边的下界抵消了,还剩下dif[i]的流量必须要流过来(否则不满足入边的下界条件),这时从源点向i连一条容量为dif[i]的边来表示即可,如果dif[i]<0,同理应该从i向汇点连一条容量为-dif[i

ZOJ 2314 带上下界的可行流

对于无源汇问题,方法有两种. 1 从边的角度来处理. 新建超级源汇, 对于每一条有下界的边,x->y, 建立有向边 超级源->y ,容量为x->y下界,建立有向边 x-> 超级汇,容量为x->y下界.建立有向边 x->y,容量为x->y的上界减下界. 2 从点的角度来处理. 新建超级源汇,对于每个点流进的下界和为 in, 流出此点的下界和为out.如果in > out. 建立有向边 超级源->i,容量为in-out.反之,建立有向边 i->超级汇

POJ 2396 Budget (有源汇有上下界的可行流)

POJ 2396 Budget 链接:http://poj.org/problem?id=2396 题意:给定一个M*N的矩阵,给定每行每列的和,以及其中一些值的限定条件,问能否构成一个可行的矩阵. 思路: 添加一个源点,向每行连边,每条边的上下界都为该行的和:添加一个汇点,每列向汇点连边,边的上下界都为该列的和.然后每行向每列连边,边的上下界一开始为(0,INF),之后通过一些限定条件更新. 现在问题成了求一个有源汇有上下界的可行流.只需要再添加一个超级源点,一个超级汇点,并且将原图的汇点向源