题意:有一些物品,一些集合,买每个物品需要一定 的费用,每个集合包含一些物品,如果集合内物品都被 买了,就会得到一定价值,求能得到的最大价值。
首先本题的模型是最大权闭合子图,而且是裸的……
问题一般情况:选择某个东西可以得到一定收益,但选了它就必须选择它的一些后继,而不同物品可能有一些相同后继,求能选到的最大收益。
这种类型的题大多都可以用这个模型做。
先对所有的电视频道的点向t连边,容量为电视频道的花费。
再从s向所有的亲戚连边,容量为亲戚给的收益。
然后每个亲戚和对应的所有电视频道连边,容量为inf。
这样,跑出来的s-t最大流就是s-t图的最小割的边权容量。然后用总收益减去这个最小割的值就是答案。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
#include<map>
#define LL long long
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=2e4+5;
inline void _read(int &x){
char t=getchar();bool sign=true;
while(t<'0'||t>'9')
{if(t=='-')sign=false;t=getchar();}
for(x=0;t>='0'&&t<='9';t=getchar())x=x*10+t-'0';
if(!sign)x=-x;
}
struct Edge{
int from,to,cap,flow;
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;
vector<int>G[maxn];
bool vis[maxn];
int d[maxn],cur[maxn];
bool bfs(){
memset(vis,0,sizeof(vis));
queue<int>q;
q.push(s);
d[s]=0,vis[s]=1;
while(q.size()){
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]=1;
d[e.to]=d[x]+1;
q.push(e.to);
}
}
}
return vis[t];
}
void add_edges(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);
}
int dfs(int x,int a){
if(x==t||a==0)return a;
int flow=0,f;
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;
}
}
return flow;
}
int maxflow(int s,int t){
this->s=s;this->t=t;
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow+=dfs(s,inf);
}
return flow;
}
}Graph;
int n,m,w[maxn],c[maxn],sum;
int main(){
_read(n);_read(m);
for(int i=1;i<=m;i++){
int x;
_read(x);
Graph.add_edges(n+i,n+m+1,x);
}
for(int i=1;i<=n;i++){
int x,y,k;
_read(x);_read(k);
sum+=x;
Graph.add_edges(0,i,x);
for(int j=1;j<=k;j++){
_read(y);
Graph.add_edges(i,n+y,inf);
}
}
cout<<sum-Graph.maxflow(0,n+m+1);
}