引入闭合子图的概念 : 通俗点说就是选出一个图的子图,使得子图中的所有点出度指向的点依旧在这个子图内,则说明此子图是闭合子图。
最大权闭合子图 : 假设每个点具有点权值,在一个图的所有闭合子图中,点权之和最大的即是最大权闭合子图。
求取最大权闭合子图的权值之和是有一个结论的
一、先抽象出一个超级源、汇点
二、将权值为正的点和超级源点连接、容量为权值
三、将权值为负的点和超级汇点连接、容量为权值的绝对值
四、然后除了源、汇之外的点原本怎么连泽怎么连、且容量为无穷大
五、最大权闭合子图权值 = 所有权值为正的权值之和 - 最大流
一些题目
分析 : 如果要选择一个带有收益的边作为收益的话,那么就要同时选择其两个端点,如果将边抽象出来化成一个点的话,那么就是要选择一些点且这些
点要构成闭合子图,所以就是求取最大权闭合子图,那么就将每条边抽象成一个点,在处理每个边的信息的时候 (u, v, w) ,连接源点和这条边的化成的
点连接容量为 w ,然后从这个边化成的点连向 u、v 容量为 INF,构成限制关系,即选择这个边化成的点就必须同时选择 u 和 v,最后将所有的点和汇
点连接,权值是点权的绝对值
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from,to,cap,flow;
Edge(){}
Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
};
struct Dinic
{
int n,m,s,t; //结点数,边数(包括反向弧),源点与汇点编号
vector<Edge> edges; //边表 edges[e]和edges[e^1]互为反向弧
vector<int> G[maxn]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn]; //BFS使用,标记一个节点是否被遍历过
int d[maxn]; //d[i]表从起点s到i点的距离(层次)
int cur[maxn]; //cur[i]表当前正访问i节点的第cur[i]条弧
void init(int n,int s,int t)
{
this->n=n,this->s=s,this->t=t;
for(int i=0;i<=n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,0) );
edges.push_back( Edge(to,from,0,0) );
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int> Q;//用来保存节点编号的
Q.push(s);
d[s]=0;
vis[s]=true;
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=0; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to] = d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
//a表示从s到x目前为止所有弧的最小残量
//flow表示从x到t的最小残量
int DFS(int x,int a)
{
if(x==t || a==0)return a;
int flow=0,f;//flow用来记录从x到t的最小残量
for(int& i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=DFS( e.to,min(a,e.cap-e.flow) ) )>0 )
{
e.flow +=f;
edges[G[x][i]^1].flow -=f;
flow += f;
a -= f;
if(a==0) break;
}
}
if(!flow) d[x] = -1;///炸点优化
return flow;
}
int Maxflow()
{
int flow=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
}DC;
int main(void)
{
int N, M;
scanf("%d %d", &N, &M);
int S = 0;
int T = N+M+1;
DC.init(N+M+2, S, T);
for(int i=1; i<=N; i++){
int Cost;
scanf("%d", &Cost);
DC.AddEdge(i, T, Cost);
}
int tot = 0;
for(int i=1; i<=M; i++){
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
DC.AddEdge(S, N+i, w);
DC.AddEdge(N+i, u, INF);
DC.AddEdge(N+i, v, INF);
tot += w;
}
printf("%dn", tot - DC.Maxflow());
return 0;
}
View Code
分析 : 裸题、用来验模板吧
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from,to,cap,flow;
Edge(){}
Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
};
struct Dinic
{
int n,m,s,t; //结点数,边数(包括反向弧),源点与汇点编号
vector<Edge> edges; //边表 edges[e]和edges[e^1]互为反向弧
vector<int> G[maxn]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn]; //BFS使用,标记一个节点是否被遍历过
int d[maxn]; //d[i]表从起点s到i点的距离(层次)
int cur[maxn]; //cur[i]表当前正访问i节点的第cur[i]条弧
void init(int n,int s,int t)
{
this->n=n,this->s=s,this->t=t;
for(int i=0;i<=n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,0) );
edges.push_back( Edge(to,from,0,0) );
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int> Q;//用来保存节点编号的
Q.push(s);
d[s]=0;
vis[s]=true;
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=0; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to] = d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
//a表示从s到x目前为止所有弧的最小残量
//flow表示从x到t的最小残量
int DFS(int x,int a)
{
if(x==t || a==0)return a;
int flow=0,f;//flow用来记录从x到t的最小残量
for(int& i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=DFS( e.to,min(a,e.cap-e.flow) ) )>0 )
{
e.flow +=f;
edges[G[x][i]^1].flow -=f;
flow += f;
a -= f;
if(a==0) break;
}
}
if(!flow) d[x] = -1;///炸点优化
return flow;
}
int Maxflow()
{
int flow=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
}DC;
int arr[maxn];
int main(void)
{
int N;
scanf("%d", &N);
int S = 0, T = N + 1;
DC.init(N+2, S, T);
int tot = 0;
for(int i=1; i<=N; i++){
int A, B;
scanf("%d %d", &A, &B);
arr[i] = A - B;
if(arr[i] >= 0) DC.AddEdge(S, i, arr[i]), tot += arr[i];
else DC.AddEdge(i, T, -arr[i]);
}
int u, v, num = 0;
while(~scanf("%d %d", &u, &v))
DC.AddEdge(v, u, INF);
printf("%dn", tot - DC.Maxflow());
return 0;
}
View Code
分析 : 也差不多是裸题了,利用结论就行了,注意需要用 long long
#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 1e6 + 10;
const int INF = 0x3f3f3f3f;
struct Edge
{
int from,to,cap;
LL flow;
Edge(){}
Edge(int from,int to,int cap,LL flow):from(from),to(to),cap(cap),flow(flow){}
};
struct Dinic
{
int n,m,s,t; //结点数,边数(包括反向弧),源点与汇点编号
vector<Edge> edges; //边表 edges[e]和edges[e^1]互为反向弧
vector<int> G[maxn]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn]; //BFS使用,标记一个节点是否被遍历过
int d[maxn]; //d[i]表从起点s到i点的距离(层次)
int cur[maxn]; //cur[i]表当前正访问i节点的第cur[i]条弧
void init(int n,int s,int t)
{
this->n=n,this->s=s,this->t=t;
for(int i=0;i<=n;i++) G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back( Edge(from,to,cap,0) );
edges.push_back( Edge(to,from,0,0) );
m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
memset(vis,0,sizeof(vis));
queue<int> Q;//用来保存节点编号的
Q.push(s);
d[s]=0;
vis[s]=true;
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=0; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to] = d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
//a表示从s到x目前为止所有弧的最小残量
//flow表示从x到t的最小残量
LL DFS(int x,LL a)
{
if(x==t || a==0)return a;
LL flow=0,f;//flow用来记录从x到t的最小残量
for(int& i=cur[x]; i<G[x].size(); i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=DFS( e.to,min(a,e.cap-e.flow) ) )>0 )
{
e.flow +=f;
edges[G[x][i]^1].flow -=f;
flow += f;
a -= f;
if(a==0) break;
}
}
if(!flow) d[x] = -1;///炸点优化
return flow;
}
LL Maxflow()
{
LL flow=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
}DC;
int main(void)
{
int nCase;
scanf("%d", &nCase);
while(nCase--){
int n, m;
scanf("%d %d", &n, &m);
DC.init(n+m+2, 0, n+m+1);
LL tot = 0;
for(int i=1; i<=n; i++){
int val;
scanf("%d", &val);
DC.AddEdge(0, i, (LL)val);
tot += (LL)val;
}
for(int i=1; i<=m; i++){
int cost;
scanf("%d", &cost);
DC.AddEdge(n+i, n+m+1, (LL)cost);
}
for(int i=1; i<=n; i++){
int n1, n2;
scanf("%d %d", &n1, &n2);
for(int j=1; j<=n1; j++){
int tmp;
scanf("%d", &tmp);
DC.AddEdge(i, n+tmp, INF);
}
for(int j=1; j<=n2; j++){
int tmp;
scanf("%d", &tmp);
DC.AddEdge(i, tmp, INF);
}
}
printf("%lldn", tot - DC.Maxflow());
}
return 0;
}
//0
//1 ~ n
//n+1 ~ n+m
//n+m+1
View Code
原文链接: https://www.cnblogs.com/qwertiLH/p/9211173.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/276407
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!