zoukankan      html  css  js  c++  java
  • [USACO09MAR]地震损失2Earthquake Damage 2

    题意

    一共有P个牧场.由C条双向路连接.两个牧场间可能有多条路.一条路也可能连接相同的牧场.牛棚坐落在牧场1.

    N (1 <= N <= P) 只奶牛打来了求救电话,说她们的农场没有被摧毁,但是已经无法到达牛棚. 求出最少可能有多少牧场被摧毁.


    求最小割
    没有被摧毁的连INF
    注意这里的是点,要拆成两个才能转化成边(因此WA了几次


    # include <bits/stdc++.h>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    # define Copy(a, b) memcpy(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(6010), __(1e5 + 10), INF(1e8);
    
    IL ll Read(){
        RG char c = getchar(); RG ll x = 0, z = 1;
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
        return x * z;
    }
    
    int p, c, n, fst[_], nxt[__], to[__], cnt, S, T, lev[_], max_flow, w[__], cur[_], vis[_];
    queue <int> Q;
    
    IL void Add(RG int u, RG int v, RG int f){  w[cnt] = f; to[cnt] = v; nxt[cnt] = fst[u]; fst[u] = cnt++;  }
    
    IL int Dfs(RG int u, RG int maxf){
        if(u == T) return maxf;
        RG int ret = 0;
        for(RG int e = fst[u]; e != -1; e = nxt[e]){
            if(lev[to[e]] != lev[u] + 1 || !w[e]) continue;
            RG int f = Dfs(to[e], min(w[e], maxf - ret));
            ret += f; w[e ^ 1] += f; w[e] -= f;
            if(ret == maxf) break;
        }
        if(!ret) lev[u] = 0;
        return ret;
    }
    
    IL bool Bfs(){
        Q.push(S); Fill(lev, 0); lev[S] = 1;
        while(!Q.empty()){
            RG int u = Q.front(); Q.pop();
            for(RG int e = fst[u]; e != -1; e = nxt[e]){
                if(lev[to[e]] || !w[e]) continue;
                lev[to[e]] = lev[u] + 1;
                Q.push(to[e]);
            }
        }
        return lev[T];
    }
    
    int main(RG int argc, RG char* argv[]){
        Fill(fst, -1); p = Read(); c = Read(); n = Read();
        T = p + p + 1; S = 1; Add(S, S + p, INF); Add(S + p, S, 0);
        for(RG int i = 1, a, b; i <= c; i++) a = Read(), b = Read(), Add(a + p, b, INF), Add(b + p, a, INF);
        for(RG int i = 1; i <= n; i++) vis[Read()] = 1;
        for(RG int i = 1; i <= p; i++)
            if(!vis[i]) Add(i, i + p, 1), Add(i + p, i, 0);
            else Add(i, i + p, INF), Add(i + p, i, 0), Add(i + p, T, INF), Add(T, i + p, 0);
        while(Bfs()) Copy(cur, fst), max_flow += Dfs(S, INF);
        printf("%d
    ", max_flow);
        return 0;
    }
    
  • 相关阅读:
    抽象与接口的综合练习
    java构造函数能否被继承,为什么?
    题解 【USACO 4.2.1】草地排水
    题解 【NOI2010】超级钢琴
    题解 [USACO Mar08] 奶牛跑步
    题解 【NOIP2016】魔法阵
    题解 对称二叉树
    题解 【NOIP2014】解方程
    题解 【NOIP2010】关押罪犯
    题解 贪吃蛇
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206351.html
Copyright © 2011-2022 走看看