• [ZJOI2009]狼和羊的故事


    裸的最小割然而我还是太菜了,没看见有零的格子,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))
    # define ID(a, b) (a - 1) * m + b
    using namespace std;
    typedef long long ll;
    
    const int _(10010), __(6e6 + 10), INF(2147483647);
    
    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 n, m, mp[110][110], w[__], fst[_], nxt[__], to[__], cnt, S, T, lev[_], cur[_], max_flow;
    int dx[] = {1, -1}, dy[] = {0, 0, 1, -1};
    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++;
        w[cnt] = 0; to[cnt] = u; nxt[cnt] = fst[v]; fst[v] = cnt++;
    }
    
    IL int Dfs(RG int u, RG int maxf){
        if(u == T) return maxf;
        RG int ret = 0;
        for(RG int &e = cur[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(){
        Fill(lev, 0); lev[S] = 1; Q.push(S);
        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); n = Read(); m = Read(); T = n * m + 1;
        for(RG int i = 1; i <= n; ++i)
            for(RG int j = 1; j <= m; ++j){
                mp[i][j] = Read();
                if(mp[i][j] == 1) Add(ID(i, j), T, INF);
                if(mp[i][j] == 2) Add(S, ID(i, j), INF);
            }
        for(RG int i = 1; i <= n; ++i)
            for(RG int j = 1; j <= m; ++j){
                if(mp[i][j] == 1) continue;
                for(RG int d = 0; d < 4; d++){
                    RG int x = i + dx[d], y = j + dy[d];
                    if(x < 1 || y < 1 || x > n || y > m || mp[x][y] == 2) continue;
                    Add(ID(i, j), ID(x, y), 1);
                }
            }
        while(Bfs()) Copy(cur, fst), max_flow += Dfs(S, INF);
        printf("%d
    ", max_flow);
        return 0;
    }
    
  • 相关阅读:
    GO开发[一]:golang语言初探
    Python带参数的装饰器
    Python函数篇
    19. logging模块
    18. json模块
    17. os模块
    16. sys模块
    15. random模块
    14. 模块-time模块
    29. java面向对象项目
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206350.html
Copyright © 2020-2023  润新知