zoukankan      html  css  js  c++  java
  • POJ 1966 Cable TV Network (算竞进阶习题)

    拆点+网络流

    拆点建图应该是很常见的套路了。。一张无向图不联通,那么肯定有两个点不联通,但是我们不知道这两个点是什么。
    所以我们枚举所有点,并把每个点拆成入点和出点,每次把枚举的两个点的入点作为s和t(这样方便,当然也可以把第一个点的出点当成s,第二个点的入点当成t,但其实我们把s和t的入点和出点之间的边容量设为INF之后就没有影响了)
    每条原图的边连接着u的出点和v的入点,v的出点和u的入点,容量设为INF,保证不给割,其他点的入点和出点之间的容量当然是1。这样我们的割就一定会割在容量为1的边上,每割一条,就相当于去掉了一个点。暴力跑最大流早最小值就好了

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 1000;
    int n, m, cnt, head[N<<2], x[N<<2], y[N<<2], s, t, depth[N<<2], ans;
    struct Edge { int v, next, f; } edge[N<<5];
    
    void addEdge(int a, int b, int f){
        edge[cnt].v = b, edge[cnt].f = f, edge[cnt].next = head[a], head[a] = cnt ++;
        edge[cnt].v = a, edge[cnt].f = 0, edge[cnt].next = head[b], head[b] = cnt ++;
    }
    
    bool bfs(){
        full(depth, 0);
        queue<int> q;
        depth[s] = 1, q.push(s);
        while(!q.empty()){
            int cur = q.front(); q.pop();
            //cout << cur << endl;
            for(int i = head[cur]; i != -1; i = edge[i].next){
                int u = edge[i].v;
                if(!depth[u] && edge[i].f > 0){
                    depth[u] = depth[cur] + 1;
                    q.push(u);
                }
            }
        }
        return depth[t] != 0;
    }
    
    int dfs(int cur, int a){
        if(cur == t) return a;
        int flow = 0;
        for(int i = head[cur]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(depth[u] == depth[cur] + 1 && edge[i].f > 0){
                int k = dfs(u, min(a, edge[i].f));
                a -= k, flow += k, edge[i].f -= k, edge[i^1].f += k;
            }
            if(!a) break;
        }
        if(a) depth[cur] = -1;
        return flow;
    }
    
    int dinic(){
        int ret = 0;
        while(bfs()) ret += dfs(s, INF);
        return ret;
    }
    
    void solve(){
        full(head, -1), cnt = 0;
        for(int i = 0; i < n; i ++){
            if(i == s || i == t) addEdge(i, i + n, INF);
            else addEdge(i, i + n, 1);
        }
        for(int i = 0; i < m; i ++){
            addEdge(x[i] + n, y[i], INF), addEdge(y[i] + n, x[i], INF);
        }
        ans = min(ans, dinic());
    }
    
    int main(){
    
        while(scanf("%d%d", &n, &m) != EOF){
            for(int i = 0; i < m; i ++){
                int j; char str[100]; scanf("%s", str);
                for(x[i] = 0, j = 1; str[j] != ','; j ++) x[i] = x[i] * 10 + (str[j] - '0');
                for(y[i] = 0, j ++; str[j] != ')'; j ++) y[i] = y[i] * 10 + (str[j] - '0');
            }
            ans = INF;
            for(int i = 0; i < n; i ++){
                for(int j = i + 1; j < n; j ++){
                    s = i, t = j, solve();
                }
            }
            if(n <= 1 || ans == INF) ans = n;
            printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    Learning NFS/NIS 2nd 读书笔记-Chapter3 NIS Operation
    Linux Enterprise Cluster Notes Ch11 LVS Introduction Theory
    Linux Enterprise Cluster NOtes Ch7 A Sample HA config
    Linux Enterprise Cluster Notes Ch10 build a Linux cluster
    Linux Enterprise Cluster NOtes Ch8 Heartbeat配置和维护
    当被监控的应用发生问题时,heartbeat会failover么?
    Linux Enterprise Cluster NOtes Ch9 Stonith and IPFail
    Linux Enterprise Cluster NOtes Ch6 Heartbeat介绍和原理
    客户端不支持javascript怎么办
    js 返回对象|js返回多个值的方法|js如何返回多个值
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10685804.html
Copyright © 2011-2022 走看看