您的位置:首页 > 其它

bzoj1391 [Ceoi2008]order

2017-12-25 11:27 459 查看
(http://www.elijahqi.win/2017/12/25/bzoj1391-ceoi2008order/)

Description

有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成。 现在给出这些参数,求最大利润

Input

第一行给出 N,M(1<=N<=1200,1<=M<=1200) 下面将有N块数据,每块数据第一行给出完成这个任务能赚到的钱(其在[1,5000])及有多少道工序 接下来若干行每行两个数,分别描述完成工序所需要的机器编号及租用它的费用(其在[1,20000]) 最后M行,每行给出购买机器的费用(其在[1,20000])

Output

最大利润

Sample Input

2 3

100 2

1 30

2 20

100 2

1 40

3 80

50

80

110

Sample Output

50

HINT

这题和loj6001很像 建图的时候只需要 把任务向仪器建租金的边 然后 仪器向汇建买的边 然后源向任务建报酬的边 跑最小割即可

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 2500
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0;char ch=gc();
while(ch<'0'||ch>'9') ch=gc();
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x;
}
struct node{
int y,z,next;
}data[4400000];
int h
,level
,cur
,num=1,n,m,T;
inline void insert1(int x,int y,int z){
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;
}
inline bool bfs(){
memset(level,0,sizeof(level));level[0]=1;queue<int>q;q.push(0);
while(!q.empty()){
int x=q.front();q.pop();
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if (level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if (y==T) return 1;
}
}return 0;
}
inline int dfs(int x,int s){
if (x==T) return s;int ss=s,tt=h[x];
for (int i=cur[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if(level[x]+1==level[y]&&z){
int xx=dfs(y,min(s,z));if (!xx) level[y]=0;tt=i;
s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) {cur[x]=i;return ss;}
}
}cur[x]=tt;return ss-s;
}
int main(){
freopen("bzoj1391.in","r",stdin);
n=read();m=read();int sum=0;T=n+m+1;
for (int i=1;i<=n;++i){
int pay=read(),k=read();sum+=pay;insert1(0,i,pay);
for (int j=1;j<=k;++j){
int x=read(),cost=read();
insert1(i,n+x,cost);
}
}for (int i=1;i<=m;++i) insert1(n+i,T,read());int ans=0;
while(bfs()) {memcpy(cur,h,sizeof(cur));ans+=dfs(0,inf);}printf("%d",sum-ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: