CodeForces 1082 G Petya and Graph 最大权闭合子图。

题目传送门

题意:现在有一个图,选择一条边,会把边的2个顶点也选起来,最后会的到一个边的集合 和一个点的集合 , 求边的集合 - 点的集合最大是多少。

题解:裸的最大权闭合子图。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod =  (int)1e9+7;
const int N = 2e3 + 100, M = 1e5;
int head[N], deep[N], cur[N];
int w[M], to[M], nx[M];
int tot;
void add(int u, int v, int val){
    w[tot]  = val; to[tot] = v;
    nx[tot] = head[u]; head[u] = tot++;

    w[tot] = 0; to[tot] = u;
    nx[tot] = head[v]; head[v] = tot++;
}
int bfs(int s, int t){
    queue<int> q;
    memset(deep, 0, sizeof(deep));
    q.push(s);
    deep[s] = 1;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; ~i; i = nx[i]){
            if(w[i] > 0 && deep[to[i]] == 0){
                deep[to[i]] = deep[u] + 1;
                q.push(to[i]);
            }
        }
    }
    return deep[t] > 0;
}
int Dfs(int u, int t, int flow){
    if(u == t) return flow;
    for(int &i = cur[u]; ~i; i = nx[i]){
        if(deep[u]+1 == deep[to[i]] && w[i] > 0){
            int di = Dfs(to[i], t, min(w[i], flow));
            if(di > 0){
                w[i] -= di, w[i^1] += di;
                return di;
            }
        }
    }
    return 0;
}

LL Dinic(int s, int t){
    LL ans = 0, tmp;
    while(bfs(s, t)){
        for(int i = 0; i <= t; i++) cur[i] = head[i];
        while(tmp = Dfs(s, t, inf)) ans += tmp;
    }
    return ans;
}
void init(){
    memset(head, -1, sizeof(head));
    tot = 0;
}
int main(){
    int n, m, s, t;
    scanf("%d%d", &n, &m);
    init();
    s = 0; t = m+n+1;
    for(int i = 1, v; i <= n; ++i){
        scanf("%d", &v);
        add(i,t,v);
    }
    LL ans = 0;
    for(int i = 1, v, u, w; i <= m; ++i){
        scanf("%d%d%d", &u, &v, &w);
        add(n+i, u, inf);
        add(n+i, v, inf);
        add(s, n+i, w);
        ans += w;
    }
    ans -= Dinic(s,t);
    printf("%lld\n", ans);
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/MingSD/p/10052857.html