[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\n", max_flow);
    return 0;
}
posted @ 2017-12-29 09:36  Cyhlnj  阅读(184)  评论(0编辑  收藏  举报