Cable TV Network UVA - 1660(拆点法+最小割)

传送门

题意:给定一个n个点的无向图,求它的点联通度,即最少删除多少个点,使得图不连通。

题解:可以想到用最小割来做,把一个点拆成i和i+N,中间连一条容量为1的边,然后无向边连INF的容量,最后枚举i+N与j求出最小值即可,注意每次枚举都要重新建图,因为跑过一次后就成0了。

附上代码:


#include<bits/stdc++.h>

using namespace std;

const int maxn=100+50;
const int INF=0x3f3f3f3f;

int N,M;

struct Edge {
  int from, to, cap, flow;
  Edge(int u, int v, int c, int f):from(u),to(v),cap(c),flow(f) {}
};

struct Edge1{
    int u,v;
};
vector<Edge1>es;

struct EdmondsKarp {
  int n, m;
  vector<Edge> edges;    // 边数的两倍
  vector<int> G[maxn];   // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
  int a[maxn];           // 当起点到i的可改进量
  int p[maxn];           // 最短路树上p的入弧编号

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

  void AddEdge(int from, int to, int 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);
  }

  int Maxflow(int s, int t) {
    int flow = 0;
    for(;;) {
      memset(a, 0, sizeof(a));
      queue<int> Q;
      Q.push(s);
      a[s] = INF;
      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(!a[e.to] && e.cap > e.flow) {
            p[e.to] = G[x][i];
            a[e.to] = min(a[x], e.cap-e.flow);
            Q.push(e.to);
          }
        }
        if(a[t]) break;
      }
      if(!a[t]) break;
      for(int u = t; u != s; u = edges[p[u]].from) {
        edges[p[u]].flow += a[t];
        edges[p[u]^1].flow -= a[t];
      }
      flow += a[t];
    }
    return flow;
  }
};

EdmondsKarp g;

int solve()
{
    if(N==0){
        return 0;
    }
    if(N==1){
        return 1;
    }
    int ans=N;
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            if(i!=j){
                g.init(N*2);
                for(int k=0;k<N;k++){
                    g.AddEdge(k,k+N,1);
                }
                for(auto&e :es){
                    g.AddEdge(e.u+N,e.v,INF);
                    g.AddEdge(e.v+N,e.u,INF);
                }
                ans=min(ans,g.Maxflow(i+N,j));
            }
        }
    }
    return ans;
}

int main()
{
    while(scanf("%d%d",&N,&M)!=EOF){
        Edge1 e;
        es.clear();
        for(int i=0;i<M;i++){
            scanf(" (%d,%d)",&e.u,&e.v);
            es.push_back(e);
        }
        int ans=solve();
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/85253508
今日推荐