UVA 1660 - Cable TV Network (最小割)

题目链接:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=0&problem=4535&mosmsg=Submission+received+with+ID+26565007

点的容量问题可以用拆点解决,于是将每个点拆成两个点,连容量为 \(1\) 的边,原来的边容量为无穷大,

表示只能割点,割点的代价为 \(1\),枚举点对,其中使得点对不连通的最小割的最小值即为答案

特殊情况:当点对之间两两都有边时,最小割为无穷大,此时的点连通度为 \(n\)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 510;
const int INF = 1000000007;

int n, m;
int u[maxn], v[maxn];

int h[maxn], cnt = 1;
struct E{
	int to, cap, next;
}e[maxn * maxn * 10];
void add(int u, int v, int c){
	e[++cnt].to = v;
	e[cnt].cap = c;
	e[cnt].next = h[u];
	h[u] = cnt;
}

int s,t;
int vis[maxn], d[maxn], cur[maxn];

  bool BFS() {
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    d[s] = 0;
    while(!Q.empty()) {
      int u = Q.front(); Q.pop();
      for(int i = h[u]; i != -1 ; i = e[i].next) {
        if(!vis[e[i].to] && e[i].cap) {
          vis[e[i].to] = 1;
          d[e[i].to] = d[u] + 1;
          Q.push(e[i].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 != -1 ; i = e[i].next) {
      if(d[x] + 1 == d[e[i].to] && (f = DFS(e[i].to, min(a, e[i].cap))) > 0) {
        e[i].cap -= f;
        e[i^1].cap += f;
        flow += f;
        a -= f;
        if(a == 0) break;
      }
    }
    return flow;
  }

  int Maxflow() {
    int flow = 0;
    while(BFS()) {
	  memcpy(cur, h, sizeof(h));
      flow += DFS(s, INF);
    }
    return flow;
  }

ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }

int main(){
	int kase = 0; int flag = 0;
	while(scanf("%d%d", &n, &m) == 2){
		memset(h, -1, sizeof(h)); cnt = 1;
		
		for(int i = 1 ; i <= m ; ++i){
			scanf(" (%d,%d)", &u[i], &v[i]);
			++u[i], ++v[i];
		}
		
		int ans = INF;
		
		for(int ss = 1 ; ss <= n ; ++ss){
			for(int tt = 1 ; tt <= n ; ++tt){
				if(ss == tt) continue;
				
				memset(h, -1, sizeof(h)); cnt = 1;
				for(int i = 1 ; i <= n ; ++i){
					add(i, i + n, 1);
					add(i + n, i, 0);
				}
				for(int i = 1 ; i <= m ; ++i){
					add(u[i] + n, v[i], INF);
					add(v[i], u[i] + n, 0);
					add(v[i] + n, u[i], INF);
					add(u[i], v[i] + n, 0);
				}
				
				s = ss + n, t = tt;
				ans = min(ans, Maxflow());
			}
		}

		if(ans == INF) ans = n;
		printf("%d\n", ans);
	} 
	return 0;
}
上一篇:NJU Static Program Analysis 06: Data Flow Analysis IV


下一篇:NJU Static Program Analysis 03: Data Flow Analysis I