网络流 - order(BZOJ 1391)

传送门


Analysis

建立虚点,向每个工作连一条流量为该工作可以赚的钱数
每个工作向其需要的机器连接流量为租金的边
每个机器向汇点连接一条流量为购买费用的边
然后跑最大流

至于为什么这样可以做
可以同这道题类比

Code
#include<bits/stdc++.h>
#define in read()
#define re register
using namespace std;
inline int read(){
	char ch;int f=1,res=0;
	while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
	while(ch>='0'&&ch<='9'){
		res=(res<<1)+(res<<3)+(ch^48);
		ch=getchar();
	}
	return f==1?res:-res;
}
const int inf = 1e9;
const int N=3000,M=4e6;
int n,m;
int S,T;
int lev[N],cur[N];
int nxt[M],to[M],cap[M],ecnt=1,head[N];
inline void add(int x,int y,int w){
	nxt[++ecnt]=head[x];head[x]=ecnt;to[ecnt]=y;cap[ecnt]=w;
	nxt[++ecnt]=head[y];head[y]=ecnt;to[ecnt]=x;cap[ecnt]=0;
}
bool bfs(){
	queue<int> q;
	for(re int i=S;i<=T;++i) lev[i]=-1,cur[i]=head[i];
	q.push(S);lev[S]=0;
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(re int e=head[u];e;e=nxt[e]){
			int v=to[e];
			if(lev[v]!=-1||cap[e]<=0) continue;
			lev[v]=lev[u]+1;
			if(v==T) return 1;
			q.push(v);
		}
	}
	return 0;
}
int dinic(int u,int flow){
	if(u==T) return flow;
	int delta,res=0;
	for(re int &e=cur[u];e;e=nxt[e]){
		int v=to[e];
		if(lev[v]>lev[u]&&cap[e]){
			delta=dinic(v,min(flow-res,cap[e]));
			if(delta){
				cap[e]-=delta;cap[e^1]+=delta;
				res+=delta;if(res==flow) return res;
			}
		}
	}
	return res;
}
int main(){
	int sum=0;
	n=in;m=in;S=0;T=n+m+1;
	for(re int i=1;i<=n;++i){
		int get=in,num=in;
		add(0,i,get);sum+=get;
		for(re int j=1;j<=num;++j){
			int id=in,c=in;
			add(i,id+n,c);
		}
	}
	for(re int i=1;i<=m;++i){
		int v=in;
		add(n+i,T,v);
	}
	int maxflow=0;
	while(bfs()) maxflow+=dinic(S,inf);
	cout<<sum-maxflow;
	return 0;
}


后记

stO hxy Orz
(感谢dalao带我飞︿( ̄︶ ̄)︿)
网上题解清一色“简单的建图”
但……一开始我并没有想出来
本想弃了,看代码
但是%%%hxy%%%鼓励我想想想想
然后~~~~
嘻嘻嘻,同太空飞行计划就做出来啦
果然要多见,多思!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值