zoukankan      html  css  js  c++  java
  • [HEOI2016]游戏

    二分图匹配(网络流实现)

    # 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 _(10010), __(1e6 + 10), INF(1e9);
    
    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, w[__], fst[_], nxt[__], to[__], cnt, num = 1, id1[60][60], id2[60][60];
    int S, T, lev[_], cur[_], max_flow;
    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;
        }
        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[]){
        n = Read(); m = Read(); Fill(fst, -1); T = 1;
        for(RG int i = 1; i <= n; ++i)
            for(RG int j = 1; j <= m; ++j){
                RG char c; scanf(" %c", &c);
                if(c != '#'){
                    if(!id1[i - 1][j]) id1[i][j] = ++num, Add(S, id1[i][j], 1);
                    else id1[i][j] = id1[i - 1][j];
                    if(!id2[i][j - 1]) id2[i][j] = ++num, Add(id2[i][j], T, 1);
                    else id2[i][j] = id2[i][j - 1];
                    if(c != 'x') Add(id1[i][j], id2[i][j], 1);
                }
            }
        while(Bfs()) Copy(cur, fst), max_flow += Dfs(S, INF);
        printf("%d
    ", max_flow);
        return 0;
    }
    
  • 相关阅读:
    《C++ Primer》读书笔记—第十章 泛型算法
    悬浮在activity上的activity对话框
    android 双向滑动 稍加改进,可做成QQHD效果
    android String.format
    使用ttf字体
    UI界面设计准则
    scrollview gn gridview混合使用问题
    android Activity 之间传递复杂对象
    android程序获取手机imei方法
    android 自定义对话框
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206356.html
Copyright © 2011-2022 走看看