码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2396 Budget (有源汇有上下界最大流)

时间:2017-10-06 16:22:02      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:void   pre   二分   lib   queue   cst   ios   mes   assert   

题意:给定一个矩阵的每行的和和每列的和,以及每个格子的限制,让你求出原矩阵。

析:把行看成X,列看成Y,其实就是二分图,然后每个X到每个Y边一条边,然后加一个超级源点和汇点分别向X和Y连边,这样就形成了一个有源汇有上下界的网络,如果有最大流,那么这个矩阵就存在。

代码如下:

#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(i,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 double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 250 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
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];
  int d[maxn];
  bool vis[maxn];
  int cur[maxn];

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

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

  bool bfs(){
    ms(vis, 0);  vis[s] = 1;
    d[s] = 1;
    queue<int> q;
    q.push(s);

    while(!q.empty()){
      int x = q.front();  q.pop();
      for(int i = 0; i < G[x].sz; ++i){
        Edge &e = edges[G[x][i]];
        if(!vis[e.to] && e.cap > e.flow){
          d[e.to] = d[x] + 1;
          vis[e.to] = 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].sz; ++i){
      Edge &e = edges[G[x][i]];
      if(d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){
        e.flow += f;
        edges[G[x][i]^1].flow -= f;
        a -= f;
        flow += f;
        if(a == 0)  break;
      }
    }
    return flow;
  }

  int maxflow(int s, int t){
    this->s = s;  this-> t = t;
    int flow = 0;
    while(bfs()){ ms(cur, 0);  flow += dfs(s, INF); }
    return flow;
  }
};

Dinic dinic;
int in[maxn];
int down[maxn][maxn], up[maxn][maxn];
bool ok;

void judge(int i, int j, int v){
  if(v < down[i][j] || v > up[i][j])  ok = false;
}

int main(){
  int T;  cin >> T;
  while(T--){
    scanf("%d %d", &n, &m);
    int s = 0, t = n + m + 1;
    int S = t + 1, T = S + 1;
    FOR(i, 1, S)  FOR(j, 1, S){
      down[i][j] = -1000;
      up[i][j] = 1000;
    }
    ms(in, 0);
    dinic.init(T + 2);
    for(int i = 1; i <= n; ++i){
      int x;  scanf("%d", &x);
      in[s] -= x;  in[i] += x;
      dinic.addEdge(s, i, 0);
    }
    for(int i = 1; i <= m; ++i){
      int x;  scanf("%d", &x);
      in[i+n] -= x;  in[t] += x;
      dinic.addEdge(i+n, t, 0);
    }
    int num;  scanf("%d", &num);
    char op[5];  int u, v, c;
    ok = true;
    while(num--){
      scanf("%d %d %s %d", &u, &v, op, &c);
      if(!ok)  continue;
      if(u && v){
        if(op[0] == ‘=‘){  judge(u, v+n, c); up[u][v+n] = down[u][v+n] = c;  }
        else if(op[0] == ‘<‘) up[u][v+n] = min(up[u][v+n], c - 1);
        else down[u][v+n] = max(down[u][v+n], c + 1);
      }
      else if(u){
        for(int j = 1; j <= m; ++j){
          if(op[0] == ‘=‘){ judge(u, j+n, c); up[u][j+n] = down[u][j+n] = c; }
          else if(op[0] == ‘<‘)  up[u][j+n] = min(up[u][j+n], c - 1);
          else  down[u][j+n] = max(down[u][j+n], c + 1);
        }
      }
      else if(v){
        for(int i = 1; i <= n; ++i){
          if(op[0] == ‘=‘){ judge(i, v+n, c);  up[i][v+n] = down[i][v+n] = c; }
          else if(op[0] == ‘<‘)  up[i][v+n] = min(up[i][v+n], c - 1);
          else down[i][v+n] = max(down[i][v+n], c + 1);
        }
      }
      else{
        for(int i = 1; i <= n; ++i)
          for(int j = 1; j <= m; ++j){
            if(op[0] == ‘=‘){ judge(i, j+n, c);  up[i][j+n] = down[i][j+n] = c; }
            else if(op[0] == ‘<‘)  up[i][j+n] = min(up[i][j+n], c - 1);
            else down[i][j+n] = max(down[i][j+n], c + 1);
          }
      }
    }
    int ans = 0;
    if(!ok){ puts("IMPOSSIBLE");  goto A; }
    for(int i = 1; i <= n; ++i)
      for(int j = 1; j <= m; ++j){
        if(up[i][j+n] < down[i][j+n]) ok = false;
        dinic.addEdge(i, j+n, up[i][j+n] - down[i][j+n]);
        in[i] -= down[i][j+n];
        in[j+n] += down[i][j+n];
      }
    dinic.addEdge(t, s, INF);
    for(int i = 0; i <= t; ++i){
      if(in[i] > 0)  dinic.addEdge(S, i, in[i]), ans += in[i];
      if(in[i] < 0)  dinic.addEdge(i, T, -in[i]);
    }
    if(!ok || ans != dinic.maxflow(S, T)){ puts("IMPOSSIBLE");  goto A; }
    for(int i = 1; i <= n; ++i){
      int cur = 0;
      for(int j = 0; dinic.edges[dinic.G[i][j]].to != n+1; ++j, cur = j);
      for(int j = cur, cnt = 0; cnt < m; ++cnt, ++j)
        printf("%d%c", dinic.edges[dinic.G[i][j]].flow + down[i][cnt+n+1], " \n"[cnt+1==m]);
    }
    A:;
    if(T)  puts("");
  }
  return 0;
}

  

POJ 2396 Budget (有源汇有上下界最大流)

标签:void   pre   二分   lib   queue   cst   ios   mes   assert   

原文地址:http://www.cnblogs.com/dwtfukgv/p/7631645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!