题意
有(n)个中转站,费用为(P_i)。有(m)个用户群,第(i)个用户群可以连接第(A_i)和(B_i)个中转站,获利为(C_i)。
现在要建立一些中转站,求最大净收益(净收益 = 获利 - 花费)
思路
最大权闭合图
用户群有点权(C_i),中转站有点权(-P_i)。用户群与可连接的中转站建立有向边。
我们选取若干用户群及其连接的中转站,我们可以很容易的发现这是一个闭合图。因为中转站没有出边,用户群指向的只有中转站,并且指向的中转站必选。
然后用最大权闭合图的套路即可解决问题。
最大密度子图
将用户群看作边,为连接(A_i)和(B_i)的无向边,边权为(C_i)。中转站具有点权(-P_i)
目标是最大化(sum_{e in E'} w_e + sum_{v in V'} p_v),这对应与具有点权和边权的最大权闭合图问题中(lambda = 0)的情形。
因此建图的时候,点到汇点的边容量是(U - deg(v) - 2p_v)
代码
最大权闭合图
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 55010, M = (3 * 50000 + 5010) * 2, inf = 1e8;
int n, m, S, T;
int h[N], e[M], ne[M], f[M], idx;
int cur[N], d[N];
void add(int a, int b, int c)
{
e[idx] = b, f[idx] = c, ne[idx] = h[a], h[a] = idx ++;
e[idx] = a, f[idx] = 0, ne[idx] = h[b], h[b] = idx ++;
}
bool bfs()
{
memset(d, -1, sizeof(d));
queue<int> que;
que.push(S);
d[S] = 0, cur[S] = h[S];
while(que.size()) {
int t = que.front();
que.pop();
for(int i = h[t]; ~i; i = ne[i]) {
int ver = e[i];
if(d[ver] == -1 && f[i]) {
d[ver] = d[t] + 1;
cur[ver] = h[ver];
if(ver == T) return true;
que.push(ver);
}
}
}
return false;
}
int find(int u, int limit)
{
if(u == T) return limit;
int flow = 0;
for(int i = cur[u]; ~i && flow < limit; i = ne[i]) {
cur[u] = i;
int ver = e[i];
if(d[ver] == d[u] + 1 && f[i]) {
int t = find(ver, min(f[i], limit - flow));
if(!t) d[ver] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
int dinic()
{
int res = 0, flow;
while(bfs()) while(flow = find(S, inf)) res += flow;
return res;
}
int main()
{
scanf("%d%d", &n, &m);
memset(h, -1, sizeof(h));
S = 0, T = n + m + 1;
for(int i = 1; i <= n; i ++) {
int x;
scanf("%d", &x);
add(m + i, T, x);
}
int tot = 0;
for(int i = 1; i <= m; i ++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
tot += c;
add(i, m + a, inf);
add(i, m + b, inf);
add(S, i, c);
}
printf("%d
", tot - dinic());
return 0;
}
最大密度子图
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 5010, M = (50000 + 2 * N) * 2, inf = 1e8;
int n, m, S, T;
int h[N], e[M], ne[M], f[M], idx;
int cur[N], d[N];
int dg[N], p[N];
void add(int a, int b, int c1, int c2)
{
e[idx] = b, f[idx] = c1, ne[idx] = h[a], h[a] = idx ++;
e[idx] = a, f[idx] = c2, ne[idx] = h[b], h[b] = idx ++;
}
bool bfs()
{
memset(d, -1, sizeof(d));
queue<int> que;
que.push(S);
d[S] = 0, cur[S] = h[S];
while(que.size()) {
int t = que.front();
que.pop();
for(int i = h[t]; ~i; i = ne[i]) {
int ver = e[i];
if(d[ver] == -1 && f[i]) {
d[ver] = d[t] + 1;
cur[ver] = h[ver];
if(ver == T) return true;
que.push(ver);
}
}
}
return false;
}
int find(int u, int limit)
{
if(u == T) return limit;
int flow = 0;
for(int i = cur[u]; ~i && flow < limit; i = ne[i]) {
cur[u] = i;
int ver = e[i];
if(d[ver] == d[u] + 1 && f[i]) {
int t = find(ver, min(f[i], limit - flow));
if(!t) d[ver] = -1;
f[i] -= t, f[i ^ 1] += t, flow += t;
}
}
return flow;
}
int dinic()
{
int res = 0, flow;
while(bfs()) {
while(flow = find(S, inf)) {
res += flow;
}
}
return res;
}
int main()
{
scanf("%d%d", &n, &m);
S = 0, T = n + 1;
memset(h, -1, sizeof(h));
for(int i = 1; i <= n; i ++) {
scanf("%d", &p[i]);
p[i] *= -1;
}
for(int i = 0; i < m; i ++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c, c);
dg[a] += c, dg[b] += c;
}
int u = 0;
for(int i = 1; i <= n; i ++) u = max(u, dg[i] + 2 * p[i]);
for(int i = 1; i <= n; i ++) {
add(S, i, u, 0);
add(i, T, u - dg[i] - 2 * p[i], 0);
}
printf("%d
", (n * u - dinic()) / 2);
return 0;
}