1391: [Ceoi2008]order
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1273 Solved: 384
[ Submit][ Status][ Discuss]
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
100 2
1 30
2 20
100 2
1 40
3 80
50
80
110
Sample Output
50
HINT
Source
题解:最小割
源点->工作 容量为收益
工作->每道工序需要的机器 容量为租金
机器->汇点 容量为购买的价格
跑一遍最大流,得到最小割,答案为所有工作的收益-最小割
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define N 3000
#define M 4000000
#define inf 1000000000
using namespace std;
int n,m,t,x;
int next[M],point[N],remain[M],v[M];
int a,b,tot,num1,sum;
int deep[N],last[N],num[N],cur[N];
void add(int x,int y,int z)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z;
tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
int addflow(int s,int t)
{
int now=t; int ans=inf;
while (now!=s)
{
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while (now!=s)
{
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
void bfs(int s,int t)
{
for (int i=s;i<=t;i++) deep[i]=t;
deep[t]=0;
queue<int> p; p.push(t);
while (!p.empty())
{
int now=p.front(); p.pop();
for (int i=point[now];i!=-1;i=next[i])
if (deep[v[i]]==t&&remain[i^1])
deep[v[i]]=deep[now]+1,p.push(v[i]);
}
}
int isap(int s,int t)
{
bfs(s,t);
for (int i=s;i<=t;i++) cur[i]=point[i];
for (int i=s;i<=t;i++) num[deep[i]]++;
int now=s; int ans=0;
while (deep[s]<t)
{
if (now==t)
{
ans+=addflow(s,t);
now=s;
}
bool f=false;
for (int i=cur[now];i!=-1;i=next[i])
if (deep[v[i]]+1==deep[now]&&remain[i])
{
last[v[i]]=i;
f=true;
cur[now]=i;
now=v[i];
break;
}
if (!f)
{
int minn=t;
for (int i=point[now];i!=-1;i=next[i])
if (remain[i]) minn=min(deep[v[i]],minn);
if (!--num[deep[now]]) break;
deep[now]=minn+1;
num[deep[now]]++;
cur[now]=point[now];
if (now!=s) now=v[last[now]^1];
}
}
return ans;
}
int main()
{
scanf("%d%d",&n,&m);
tot=-1;
memset(next,-1,sizeof(next));
memset(point,-1,sizeof(point));
for (int i=1;i<=n;i++)
{
scanf("%d%d",&x,&t); sum+=x;
add(1,i+1,x);
for (int j=1;j<=t;j++)
{
scanf("%d%d",&a,&b);
add(i+1,a+n+1,b);
}
}
for (int i=1;i<=m;i++)
{
int x; scanf("%d",&x);
add(i+n+1,n+m+2,x);
}
printf("%d\n",sum-isap(1,n+m+2));
}