bzoj1565
网络流
最大权闭合子图
我已经忘记了这个东西是什么
大概是这样的,设选了a就必须选b,那么a->b 连inf,代表 强制选
如果价值为正从source连,否则向sink连,然后sum-flow就行了,sum是所有的正权和
这个题很明显是最大权闭合子图,但是中间有些点形成强连通分量挖掉就行了
#include<bits/stdc++.h> using namespace std; const int dx[] = {0, 0, -1, 1}, dy[] = {-1, 1, 0, 0}; const int N = 32 * 32, inf = 1e9; int rd() { int x = 0, f = 1; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); } while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } int top, sum, dfs_clock, n, m, k, D, source, sink, cnt = 1, tot; int in[N], c[N], st[N], mark[N], a[N], dfn[N], low[N], vis[N], d[N], head[N], iter[N], id[41][41]; vector<int> G[N], rev[N]; struct edge { int nxt, to, f; } e[N * N << 2]; bool bfs() { queue<int> q; memset(d, -1, sizeof(d)); d[source] = 0; q.push(source); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; i; i = e[i].nxt) if(d[e[i].to] == -1 && e[i].f) { d[e[i].to] = d[u] + 1; q.push(e[i].to); } } return d[sink] != -1; } int dfs(int u, int delta) { if(u == sink) return delta; int ret = 0; for(int &i = iter[u]; i && delta; i = e[i].nxt) if(d[e[i].to] == d[u] + 1 && e[i].f) { int x = dfs(e[i].to, min(delta, e[i].f)); ret += x; delta -= x; e[i].f -= x; e[i ^ 1].f += x; } return ret; } int dinic() { int ret = 0; while(bfs()) { for(int i = source; i <= sink; ++i) iter[i] = head[i]; ret += dfs(source, inf); } return ret; } void link(int u, int v, int f) { e[++cnt].nxt = head[u]; head[u] = cnt; e[cnt].to = v; e[cnt].f = f; } void insert(int u, int v, int f) { link(u, v, f); link(v, u, 0); }