题目链接:传送门
资料:学习
思路:最大权闭包子图题目,类似于二分图,两边的点集X和点集合Y,点集X包含的是全部收益计划(一个计划一个点),点集Y包含的是全部所需物品(一个物品一个点),每个收益计划点 向 该收益计划需要的全部物品各连一条边,容量都为INF;起点S 对于每个计划连一条边,容量为该计划收益;每个物品对终点T连一条边,容量物品的成本,转换问题完成
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int,ll> P;
const ll maxn=1e6+10;
const ll INF=0x3f3f3f3f3f3f3f;
const int inf=0x3f3f3f3f;
const double mod=1e-6;
const ll maxm=5e4+5;
ll n;
ll st=0,ed=102;
ll a[maxn];
ll sum;//正权的值
ll ver[maxn],edge[maxn],nex[maxn],head[maxn],now[maxn];
ll d[maxn];
ll tot=1;
void add(int x,int y,int w){
ver[++tot]=y,edge[tot]=w,nex[tot]=head[x],head[x]=tot;
ver[++tot]=x,edge[tot]=0,nex[tot]=head[y],head[y]=tot;
}
bool bfs(){
memset(d,0,sizeof(d));
queue<ll> que;
que.push(st);
d[st]=1;
now[st]=head[st];
while(!que.empty()){
ll u=que.front();
que.pop();
//cout<<u<<endl;
for(int i=head[u];i;i=nex[i]){
if(!d[ver[i]]&&edge[i]){
que.push(ver[i]);
d[ver[i]]=d[u]+1;
now[ver[i]]=head[ver[i]];
if(ver[i]==ed)return 1;
}
}
}
return 0;
}
int dfs(ll x,ll flow){
if(x==ed)return flow;
ll res=flow;
int i,k;
for(int i=now[x];i&&res;i=nex[i]){
if((d[ver[i]]==d[x]+1)&&edge[i]){
k=dfs(ver[i],min(res,edge[i]));
if(!k)d[ver[i]]=0;
edge[i]-=k;
edge[i^1]+=k;
res-=k;
}
}
now[x]=i;
return flow-res;
}
int solve(int s,int d){
int flow=0;
int maxflow=0;
while(bfs()){
while(flow=dfs(st,inf))maxflow+=flow;
//cout<<maxflow<<endl;;
}
return maxflow;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){//成本
int x,y;
cin>>x>>y;
sum+=x*y;
add(i,ed,x);
}
ll m;
cin>>m;
for(int i=1;i<=m;i++){//收益计划
ll num,val;
cin>>num>>val;
sum+=val;
add(st,i+n,val);
while(num--){
int x;
cin>>x;
add(i+n,x,inf);
}
}
cout<<sum-solve(st,ed);
return 0;
}
相似题目:P4174 [NOI2006]最大获利