# hihoCoder 最大权闭合子图

(可抽象为最大权闭合图求法的题一般是含有一些点具有一定的利益收入，另一些点具有一定的利益支出，即每个点都有一个权值。拥有某个具有收入效益的点前提是必须得拥有一些具有一定利益支出的点。求拥有哪些点能够获得最大的利益。我们可以对收入的点指向支出的点进行建边，则构建的图的每个闭合子图就是一种方式，求解其中最大权闭合子图)

(不懂的看)

Code1：

#include <algorithm>
#include <string.h>
#include <cstdio>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 405;
const int maxm = maxn*maxn;
const int BAS = 200;
struct node
{
int v, w, next;
} edge[maxm];
int n, m, all, S, T;
int val[maxn];
queue<int> q;
int rec[maxn], pre[maxn], block[maxn], dis[maxn];
inline void init()
{
no = 0; all = 0;
}
inline void add(int u, int v, int w)
{
edge[no].v = v; edge[no].w = w;

edge[no].v = u, edge[no].w = 0;
}
void mapping()
{
int a, b, x;
S = BAS*2+1, T = BAS*2+2;
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; ++i)
{
scanf("%d", &x);
}
for(int i = 1; i <= n; ++i)
{
scanf("%d %d", &a, &b);
add(S, i, a); all += a;
for(int j = 1; j <= b; ++j)
{
scanf("%d", &x);
}
}
}
void reset(int S, int T)
{
memset(dis, 0x3f, sizeof dis);
memset(block, 0, sizeof block);
q.push(S); dis[S] = 0;
while(!q.empty())
{
int top = q.front(); q.pop();
for(int k = head[top]; k != -1; k = edge[k].next)
if(dis[edge[k].v] == inf && edge[k].w)
dis[edge[k].v] = dis[top]+1, q.push(edge[k].v);
}
}
int dinic(int S, int T)
{
int ans = 0, flow = inf, top = S;
pre[S] = S;
reset(S, T);
while(dis[T] != inf)
{
int k, tmp;
for(k = head[top]; k != -1; k = edge[k].next)
{
if(edge[k].w && dis[edge[k].v]==dis[top]+1 &&
!block[edge[k].v]) break;
}
if(k != -1)
{
tmp = edge[k].v;
flow = min(flow, edge[k].w);
pre[tmp] = top, rec[tmp] = k;
top = tmp;
if(top == T)
{
ans += flow;
for(; top != S; top = pre[top])
{
edge[rec[top]].w -= flow;
edge[rec[top]^1].w += flow;
if(!edge[rec[top]].w) tmp = top;
}
top = pre[tmp], flow = inf;
for(; top != S; top = pre[top])
flow = min(flow, edge[rec[top]].w);
top = pre[tmp];
}
}
else
{
block[top] = 1;
top = pre[top];
if(block[S]) reset(S, T);
}
}
return ans;
}
int main()
{
init();
mapping();
printf("%d\n", all-dinic(S, T));
return 0;
}

Code2：

#include <algorithm>
#include <string.h>
#include <cstdio>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 410;
const int maxm = maxn*maxn;
const int BAS = 200;
struct node
{
int v, w, next;
} edge[maxm];
int N, n, m, all, S, T;
int val[maxn];
queue<int> q;
int rec[maxn], pre[maxn], now[maxn], dis[maxn], gap[maxn];
inline void init()
{
no = 0; all = 0;
}
inline void add(int u, int v, int w)
{
edge[no].v = v; edge[no].w = w;

edge[no].v = u, edge[no].w = 0;
}
void mapping()
{
int a, b, x;
S = BAS*2+1, T = BAS*2+2;
scanf("%d %d", &n, &m);
N = n+m+2;
for(int i = 1; i <= m; ++i)
{
scanf("%d", &x);
}
for(int i = 1; i <= n; ++i)
{
scanf("%d %d", &a, &b);
add(S, i, a); all += a;
for(int j = 1; j <= b; ++j)
{
scanf("%d", &x);
}
}
}
void PRE_SAP(int S, int T)
{
memset(gap, 0, sizeof gap);
memset(dis, 0, sizeof dis);
//注意这里now数组要把所有用到的标号都存过来
//初始化一定要注意把所涉及的都覆盖到
for(int i = 0; i <= T; ++i) now[i] = head[i];
q.push(T); dis[T] = 0;
while(!q.empty())
{
int top = q.front(); q.pop();
++gap[dis[top]];
for(int k = head[top]; k != -1; k = edge[k].next)
if(dis[edge[k].v] == inf && edge[k^1].w)
dis[edge[k].v] = dis[top]+1, q.push(edge[k].v);
}
}
int SAP(int S, int T)
{
int k, ans = 0, flow = inf, top = S;
pre[S] = S;
PRE_SAP(S, T);
while(dis[S] < N)
{
if(top == T)
{
ans += flow;
for(; top != S; top = pre[top])
{
edge[rec[top]].w -= flow;
edge[rec[top]^1].w += flow;
}
flow = inf;
}
for(k = now[top]; k != -1; k = edge[k].next)
{
if(edge[k].w && dis[top] == dis[edge[k].v]+1)
{
flow = min(flow, edge[k].w);
pre[edge[k].v] = top, rec[edge[k].v] = k;
now[top] = k; top = edge[k].v;
break;
}
}
if(k == -1)
{
int mins = N;
if(--gap[dis[top]] == 0) break;
k = now[top] = head[top];
for(; k != -1; k = edge[k].next)
if(edge[k].w && mins > dis[edge[k].v])
mins = dis[edge[k].v];
++gap[dis[top] = mins+1];
top = pre[top];
}
}
return ans;
}
int main()
{
init();
mapping();
printf("%d\n", all-SAP(S, T));
return 0;
}

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客