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%%%鼓励我想想想想
然后~~~~
嘻嘻嘻,同太空飞行计划就做出来啦
果然要多见,多思!!!