题解:UVA1104 芯片难题 Chips Challenge

· · 题解

UVA1104题解


#include <bits/stdc++.h>

using namespace std;

const int N = 505, INF = 1e8;

int n, a, b, ans, tmp, c[N][2], f[N], q[N * N * N], dis[N]; //c,0表示i的前驱,c,1表示i从前驱的哪条边转移过来,f表示流量,dis表示费用 

int d[N];  //表示in_i-out_i  

bool vis[N];

char ch[N][N];

struct E {
  int x, w, o, id;
};

vector<E> v[N];

void mkb(int x, int y, int w, int o) {
  if (o > 0) {
    ans += w * o;
    d[x] -= w, d[y] += w;
    mkb(y, x, w, -o);
    return;                                         //如果边权大于0则预先设它是流满的状态,建它的反向边方便后面撤回的操作 
  }
  v[x].push_back({y, w, o, (int)v[y].size()});
  v[y].push_back({x, 0, -o, (int)v[x].size() - 1});
}

void add(int x, int y, int l, int r, int o) {       //表示x向y连一条下届为l上届为r费用为o的边 
  if (r != l) {
    mkb(x, y, r - l, o);
  }
  d[x] -= l, d[y] += l; 
  ans += l * o;
}                                                 

void D(int sx, int ex) {
  for (int i = ex, x, id; i != sx; i = x) {
    x = c[i][0], id = c[i][1];
    v[x][id].w -= f[ex], v[i][v[x][id].id].w += f[ex];
  }
  ans += f[ex] * dis[ex], tmp += f[ex];
}

bool SPFA(int sx, int ex) {
  fill(dis, dis + ex + 1, -INF);
  dis[sx] = 0;
  int hh = 1, tt = 1;
  f[sx] = INF;
  for (q[hh] = sx; hh <= tt; hh++) {
    int x = q[hh];
    vis[x] = 0;
    for (int i = 0; i < v[x].size(); i++) {
      int nx = v[x][i].x, w = v[x][i].w, o = v[x][i].o + dis[x];
      if (dis[nx] < o && w) {
        dis[nx] = o;
        c[nx][0] = x, c[nx][1] = i;
        f[nx] = min(f[x], w);
        if (!vis[nx]) {
          vis[nx] = 1;
          q[++tt] = nx;
        }
      }
    }
  }
  return (dis[ex] != -INF);
}

void dinic(int sx, int ex) {
  tmp = 0;
  for (; SPFA(sx, ex); D(sx, ex)) {
  }
}                                      //常规费用流无需解释 

void solve(int Discnt) {
  int sss = -1, ooo = 0;               //能放的部件的最大值,本来就有的部件数量 
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> ch[i][j];
      if (ch[i][j] == 'C') {
        ooo++;                         //记录本来就有的部件数量 
      }
    }
  }
  for (int s = 0; s <= n; s++) {
    memset(d, 0, sizeof(d));
    ans = 0;
    for (int i = 0; i <= 2 * n + 5; i++) {
      v[i].clear();
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (ch[i][j] == 'C') {
          add(i, j + n, 1, 1, 1);      //对于每个C对应的行向对应的列连一条下届为1上届为1费用为1的边 
        } else if (ch[i][j] == '.') {
          add(i, j + n, 0, 1, 1);      //对于每个.对应的行向对应的列连一条下届为0上届为1费用为1的边
        }
      }
    }
    for (int i = 1; i <= n; i++) { 
      mkb(i + n, i, s, 0);             //完成第i行要与第i列放的部件数相同的性质 
    }
    int sx = 0, ex = 2 * n + 1, sum = 0;
    for (int i = 1; i <= 2 * n; i++) {
      if (d[i] > 0) {
        sum += d[i];
        mkb(sx, i, d[i], 0);           
      } else if (d[i] < 0) {
        mkb(i, ex, -d[i], 0);
      }
    }                                  //上下界常规建图 
    tmp = 0;
    dinic(sx, ex);                     //跑最大费用最大流 
    if (tmp != sum) {                  //如果没流满说明没有解 
      continue;
    }
    if ((ans * a / b) >= s) {
      sss = max(sss, ans);
    }
  }
  cout << "Case " << Discnt << ": ";
  if (sss == -1) {
    cout << "impossible\n";
  } else {
    cout << sss - ooo << "\n";
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  for (int o = 1; cin >> n >> a >> b, n || a || b; solve(o), o++) {
  }
  return 0;
}