zoukankan      html  css  js  c++  java
  • P2805 [NOI2009]植物大战僵尸 + 最大权闭合子图 X 拓扑排序

    传送门:https://www.luogu.org/problemnew/show/P2805

    题意

    有一个n * m的地图,你可以操纵僵尸从地图的右边向左边走,走的一些地方是有能量值的,有些地方会被一些植物保护起来不能走,只有先吃掉特定植物才能走一些地方。求最大可能拿到的能量值和

    思路

    最大权闭合子图,由于僵尸只能从一行的右边一步一步走到左边,所以每个格子向右边连一条inf的边(表示选了这个点,右边这个点必选),然后有保护的原因,从被保护的格子向保护的格子连一条inf的边。然后就是最大权闭合子图的操作,源点向正权值格子连容量为这个权值的边,负权值的格子向终点连容量为这个权值绝对值的边。

    由于图中有环的存在,我们要把环上以及环之后的点都抹去。

    然后跑一遍dinic(),算出最小割,用总的正权值 - 这个最小割就是答案。

    #include <algorithm>
    #include  <iterator>
    #include  <iostream>
    #include   <cstring>
    #include   <cstdlib>
    #include   <iomanip>
    #include    <bitset>
    #include    <cctype>
    #include    <cstdio>
    #include    <string>
    #include    <vector>
    #include     <stack>
    #include     <cmath>
    #include     <queue>
    #include      <list>
    #include       <map>
    #include       <set>
    #include   <cassert>
    
    /*
            
    ⊂_ヽ
      \\ Λ_Λ  来了老弟
       \('ㅅ')
        > ⌒ヽ
       /   へ\
       /  / \\
       レ ノ   ヽ_つ
      / /
      / /|
     ( (ヽ
     | |、\
     | 丿 \ ⌒)
     | |  ) /
    'ノ )  Lノ
    
    */
    
    using namespace std;
    #define lson (l , mid , rt << 1)
    #define rson (mid + 1 , r , rt << 1 | 1)
    #define debug(x) cerr << #x << " = " << x << "
    ";
    #define pb push_back
    #define pq priority_queue
    
    
    
    typedef long long ll;
    typedef unsigned long long ull;
    //typedef __int128 bll;
    typedef pair<ll ,ll > pll;
    typedef pair<int ,int > pii;
    typedef pair<int,pii> p3;
    
    //priority_queue<int> q;//这是一个大根堆q
    //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
    #define fi first
    #define se second
    //#define endl '
    '
    
    #define boost ios::sync_with_stdio(false);cin.tie(0)
    #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
    #define max3(a,b,c) max(max(a,b), c);
    #define min3(a,b,c) min(min(a,b), c);
    
    
    const ll oo = 1ll<<17;
    const ll mos = 0x7FFFFFFF;  //2147483647
    const ll nmos = 0x80000000;  //-2147483648
    const int inf = 0x3f3f3f3f;
    const ll inff = 0x3f3f3f3f3f3f3f3f; //18
    const int mod = 1e9+7;
    const double esp = 1e-8;
    const double PI=acos(-1.0);
    const double PHI=0.61803399;    //黄金分割点
    const double tPHI=0.38196601;
    
    
    template<typename T>
    inline T read(T&x){
        x=0;int f=0;char ch=getchar();
        while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
        while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
        return x=f?-x:x;
    }
    
    inline void cmax(int &x,int y){if(x<y)x=y;}
    inline void cmax(ll &x,ll y){if(x<y)x=y;}
    inline void cmin(int &x,int y){if(x>y)x=y;}
    inline void cmin(ll &x,ll y){if(x>y)x=y;}
    
    /*-----------------------showtime----------------------*/
    
                const int maxn = 609;
                int val[maxn],vis[maxn],du[maxn];
                vector<int>mp[maxn];
                vector<pii>ptt[maxn];//受保护的点。
                int n,m;
                void topo(){
                    queue<int>que;
                    for(int i=1; i<=n*m; i++){
                        if(du[i] == 0) que.push(i);
                    }
                    while(!que.empty()){
                        int u = que.front();    que.pop();
                        vis[u] = 1;
                        for(int i=0; i<mp[u].size(); i++){
                            int v = mp[u][i];
                            du[v] --;
                            if(du[v] == 0) que.push(v);
                        }
                    }
                }
    
                struct E{
                    int v,w;
                    int nxt;
                }edge[500009];
                int gtot = 0, head[maxn];
                void addedge(int u,int v,int w){
                    edge[gtot].v = v;
                    edge[gtot].w = w;
                    edge[gtot].nxt = head[u];
                    head[u] = gtot++;
    
                    edge[gtot].v = u;
                    edge[gtot].w = 0;
                    edge[gtot].nxt = head[v];
                    head[v] = gtot++;
                }
    
                int dis[maxn],cur[maxn];
                bool bfs(int s,int t){
                    memset(dis, inf, sizeof(dis));
                    dis[s] = 0;
                    queue<int>que;  que.push(s);
    
                    for(int i=s; i<=t; i++) cur[i] = head[i];
                    while(!que.empty()){
                        int u = que.front(); que.pop();
                        for(int i=head[u]; ~i; i = edge[i].nxt){
                            int v = edge[i].v, w = edge[i].w;
                            if(w > 0 && dis[v] > dis[u] + 1){
                                dis[v] = dis[u] + 1;
                                que.push(v);
                            }
                        }
                    }
                    return dis[t] < inf;
                }
    
                int dfs(int u,int t,int maxflow){
                    if(u == t || maxflow == 0) return maxflow;
                    for(int i=cur[u]; ~i; i = edge[i].nxt){
                        cur[u] = i;
                        int v = edge[i].v, w = edge[i].w;
                        if(w > 0 && dis[v] == dis[u] + 1) {
                            int f = dfs(v, t, min(w, maxflow));
                            if(f > 0) {
                                edge[i].w -= f;
                                edge[i^1].w += f;
                                return f;
                            }
                        }
                    }
                    return 0;
                }
                int dinic(int s,int t){
                    int flow = 0;
                    while(bfs(s, t)){
                        while(int f = dfs(s,t,inf)) flow += f;
                    }
                    return flow;
                }
    int main(){
                memset(head, -1, sizeof(head));
                scanf("%d%d", &n, &m);
                for(int i=1; i<=n; i++){
                    for(int j=1; j<=m; j++){
                        int u = (i-1)*m + j, v = u + 1;
                        scanf("%d", &val[u]);
                        if(j < m){
                            du[u]++;mp[v].pb(u);//v -> u
                        }
                        int q;  scanf("%d", &q);
                        while(q -- ){
                            int x,y;
                            scanf("%d%d", &x, &y);
                            x++,y++;
                            int p = (x-1)*m+y;
                            du[p]++; mp[u].pb(p);
                            ptt[u].pb(pii(x,y));
                        }
                    }
                }
    
                int s = 0, t = n*m+1;
    
                topo();
    
                int sum = 0;
                for(int i=1; i<=n; i++){
                    for(int j=1; j<=m; j++){
    
                        int u = (i-1)*m + j, v = u + 1;
                        if(vis[u] == 0) continue;
                        if(j < m && vis[v])addedge(u, v, inf);
                        
                        if(val[u] >= 0) addedge(s, u, val[u]), sum += val[u];
                        else addedge(u, t, -1*val[u]);
                        
                        for(int k=0; k < ptt[u].size(); k++){
                            int x = ptt[u][k].fi, y = ptt[u][k].se;
                            int p = (x - 1) * m + y;
                            addedge(p, u, inf);
                        }
                    }
                }
            
                printf("%d
    ", sum - dinic(s,t));
                return 0;
    }
    View Code
  • 相关阅读:
    Luogu6300 悔改 [FFT,阈值法]
    CF1016G Appropriate Team [Pollard-rho,FMT]
    AGC021F Trinity【计数,NTT】
    CF578F Mirror Box 【图论,Matrix-Tree】
    [ARC083]Collecting Balls
    HNCPC2019部分题解
    [LuoguP1829]Crash的文明表格(二次扫描与换根+第二类斯特林数)
    [CF960G]Bandit Blues(第一类斯特林数+分治卷积)
    [CF804F]Fake bullions
    [CF643E]Bear and Destroying Subtrees(期望,忽略误差)
  • 原文地址:https://www.cnblogs.com/ckxkexing/p/10367396.html
Copyright © 2011-2022 走看看