LOJ6046 价


本文总阅读量
Posted by yjjr's blog on January 1, 2018

标签:最小割,网络流

题目

题目传送门

这里写图片描述

分析

30分——爆搜 40分——加一个特判(全部取)(因为Pi小于0)

正解: 最小割建图: 源点S向每个减肥药连一条流量为Pi+inf的边 减肥药向其对应的药材连一条流量为inf的边 每个药材向汇点T连一条流量为inf的边 然后跑dinic 因为存在负边权,那么要加入sum计算进入减肥药的总流量,然后输出maxflow(ans)-sum即可 因为二分图有完美匹配,这表示左边任选K个点,右边都有至少K个点与之相邻, 所以不会出现得到的收益超过INF的情况

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define inf 1000000000
using namespace std;
inline ll read()
{
	ll f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int maxn=606,maxm=5e5+6;
int n,x,y,S,T,last[maxn],que[maxm],h[maxm],cnt=1;
int sum=0,ans=0;
struct edge{int to,next,v;}e[maxm];
void insert(int u,int v,int w){
	e[++cnt]=(edge){v,last[u],w};last[u]=cnt;
	e[++cnt]=(edge){u,last[v],0};last[v]=cnt;
}

bool bfs(){
	int head=0,tail=1,now;mem(h,-1);
	que[0]=S;h[S]=0;
	while(head<tail){
		now=que[head++];
		reg(now)
			if(e[i].v&&h[e[i].to]==-1){
				h[e[i].to]=h[now]+1;
				que[tail++]=e[i].to;
			}
	}
	return h[T]!=-1;
}

int dfs(int x,int f){
	if(x==T)return f;
	int w,used=0;
	reg(x)
		if(h[e[i].to]==h[x]+1){
			w=dfs(e[i].to,min(f-used,e[i].v));
			e[i].v-=w;e[i^1].v+=w;used+=w;
			if(used==f)return f;
		}
	if(!used)h[x]=-1;
	return used;
}
	
void dinic(){while(bfs())ans+=dfs(S,inf);}
int main()
{
	n=read();S=n<<1+1,T=S+1;
	rep(i,1,n){
		x=read();
		rep(j,1,x){y=read();insert(i,y+n,inf);}
	}
	rep(i,1,n)insert(i+n,T,inf);
	rep(i,1,n){
		x=read();insert(S,i,inf-x);
		sum+=inf-x;
	}
	dinic();
	cout<<ans-sum<<endl;
	return 0;
}
本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr