【BZOJ 1391】[Ceoi2008]order 最小割

236 篇文章 0 订阅
17 篇文章 0 订阅
建图s向每个任务建边,容量为收益,任务向机器建边,容量为租用机器花费,机器向汇点建边,容量为购买机器的费用,然后跑最小割,每割掉一条边就相当于放弃这个收益或者选择这个花销。
#include<cstdio>
#include<cstring>
#include<iostream>
#define maxn 4000020
#define N 2405
using namespace std;
void read(int& x){
	x=0;char c=getchar();int f=1;
	for(;c>'9'||c<'0';c=getchar())if(c=='-')f=-1;
	for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0';
	x*=f;
}
int head[N],tot,n,m,q[N*2],ans,s,t,last[N],h[N];
struct edge{int v,next,w;}e[maxn];
void adde(int a,int b,int c){
	e[tot].v=b,e[tot].next=head[a],e[tot].w=c;head[a]=tot++;
	e[tot].v=a,e[tot].next=head[b],e[tot].w=0;head[b]=tot++;
}
bool bfs(){
	int l=0,r=1;q[l]=s;
	for(int i=s;i<=t;i++)h[i]=-1;h[s]=0;
	while(l<r){
		int u=q[l++];
		for(int v,i=head[u];i!=-1;i=e[i].next)
			if(h[v=e[i].v]==-1&&e[i].w){
				h[v]=h[u]+1;
				q[r++]=v;
			}
	}
	return h[t]!=-1;
}
int dfs(int u,int f){
	if(u==t)return f;
	int used=0,w;
	for(int v,i=last[u];i!=-1;i=e[i].next)
		if(h[v=e[i].v]==h[u]+1&&e[i].w){
			last[u]=i;
			w=min(f-used,e[i].w);
			w=dfs(v,w);
			used+=w;e[i].w-=w,e[i^1].w+=w;
			if(used==f)return used;
		}
	if(!used)h[u]=-1;
	return used;
}
void dinic(){
	while(bfs()){
		for(int i=s;i<=t;i++)last[i]=head[i];
		ans-=dfs(s,1e9);
	}
}

int main(){
	memset(head,-1,sizeof(head));
	read(n),read(m);
	t=n+m+1;
	for(int a,b,c,d,i=1;i<=n;i++){
		read(a),read(b);
		adde(s,i,a);ans+=a;
		for(int j=1;j<=b;j++){
			read(c),read(d);
			adde(i,c+n,d);
		}
	}
	for(int a,i=1;i<=m;i++){
		read(a);
		adde(n+i,t,a);
	}
	dinic();
	printf("%d",ans);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值