Problem Description
The city planners plan to build N plants in the city which has M shops.
Each shop needs products from some plants to make profit of proi units.
Building ith plant needs investment of payi units and it takes ti days.
Two or more plants can be built simultaneously, so that the time for building multiple plants is maximum of their periods( ti ).
You should make a plan to make profit of at least L units in the shortest period.
Each shop needs products from some plants to make profit of proi units.
Building ith plant needs investment of payi units and it takes ti days.
Two or more plants can be built simultaneously, so that the time for building multiple plants is maximum of their periods( ti ).
You should make a plan to make profit of at least L units in the shortest period.
Input
First line contains T, a number of test cases.
For each test case, there are three integers N, M, L described above.
And there are N lines and each line contains two integers payi , ti (1<= i <= N).
Last there are M lines and for each line, first integer is proi , and there is an integer k and next k integers are index of plants which can produce material to make profit for the shop.
1 <= T <= 30
1 <= N, M <= 200
1≤L,ti≤1000000000
1≤payi,proi≤30000
For each test case, there are three integers N, M, L described above.
And there are N lines and each line contains two integers payi , ti (1<= i <= N).
Last there are M lines and for each line, first integer is proi , and there is an integer k and next k integers are index of plants which can produce material to make profit for the shop.
1 <= T <= 30
1 <= N, M <= 200
1≤L,ti≤1000000000
1≤payi,proi≤30000
Output
For each test case, first line contains a line “Case #x: t p”, x is the number of the case, t is the shortest period and p is maximum profit in t hours. You should minimize t first and then maximize p.
If this plan is impossible, you should print “Case #x: impossible”
If this plan is impossible, you should print “Case #x: impossible”
Sample Input
2 1 1 2 1 5 3 1 1 1 1 3 1 5 3 1 1
Sample Output
Case #1: 5 2 Case #2: impossible
因为要所有plant都建造了shop才获利,所以很容易想到最大权闭合子图
对于时间,我们可以采用二分操作,每次重新建图,把t<=mid的plant加入图中。
S连shop,流量为利润,总和为sum
plant连T,流量为花费
shop连到所有需要建造的plant,如果并不是所有plant都符合要求,则不将这个shop的利润加入sum
然后当前时间的最大利润就是sum-maxflow()
继续二分逼近即可
#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct plant
{
int x,t;
int p;
bool operator <(plant y) const
{
return t<y.t;
}
}px[301];
struct shop
{
int x;
int t[301];
}s[301];
int head[2001];
struct map
{
int f;
int s,t;
int next;
}a[1000001];
int edge;
int p;
int n,m;
int q[400001],d[400001];
inline void add(int s,int t,int f)
{
a[edge].next=head[s];
head[s]=edge;
a[edge].s=s;
a[edge].t=t;
a[edge].f=f;
}
inline bool bfs()
{
int l=0,r=0;
memset(q,0,sizeof(q));
r++;
q[r]=0;
memset(d,-1,sizeof(d));
d[0]=0;
int i,k;
while(l<r)
{
l++;
int k=q[l];
for(i=head[k];i!=0;i=a[i].next)
{
if(a[i].f>0&&d[a[i].t]==-1)
{
d[a[i].t]=d[k]+1;
r++;
q[r]=a[i].t;
}
}
}
if(d[p]>=0)
return true;
return false;
}
inline int dfs(int k,int s)
{
if(k==p)
return s;
int t=s;
int i;
for(i=head[k];i!=0;i=a[i].next)
{
if(d[a[i].t]==d[k]+1&&a[i].f>0)
{
int xx=dfs(a[i].t,min(s,a[i].f));
a[i].f-=xx;
if(i%2==0)
a[i-1].f+=xx;
else
a[i+1].f+=xx;
s-=xx;
}
}
if(t==s)
d[k]=-1;
return t-s;
}
inline int maxflow()
{
int s=0;
while(bfs())
s+=dfs(0,2100000000);
return s;
}
int fx[201];
inline int cale(int mid)
{
int lim=0;
p=n+m+1;
memset(head,0,sizeof(head));
edge=0;
int i,j;
int sum=0;
for(i=1;i<=n;i++)
if(px[i].t>mid)
break;
lim=i-1;
for(i=1;i<=m;i++)
{
bool flag=true;
for(j=1;j<=s[i].t[0];j++)
{
if(fx[s[i].t[j]]>lim)
{
flag=false;
break;
}
edge++;
add(i,fx[s[i].t[j]]+m,2100000000);
edge++;
add(fx[s[i].t[j]]+m,i,0);
}
if(flag)
{
edge++;
add(0,i,s[i].x);
edge++;
add(i,0,0);
sum+=s[i].x;
}
}
for(i=1;i<=lim;i++)
{
edge++;
add(i+m,p,px[i].x);
edge++;
add(p,i+m,0);
}
return sum-maxflow();
}
int main()
{
int kt=0;
int T;
scanf("%d",&T);
while(T>0)
{
kt++;
T--;
int L;
scanf("%d%d%d",&n,&m,&L);
int i,j;
int mx=0;
for(i=1;i<=n;i++)
{
scanf("%d%d",&px[i].x,&px[i].t);
mx=max(px[i].t,mx);
px[i].p=i;
}
for(i=1;i<=m;i++)
{
scanf("%d%d",&s[i].x,&s[i].t[0]);
for(j=1;j<=s[i].t[0];j++)
scanf("%d",&s[i].t[j]);
}
sort(px+1,px+1+n);
for(i=1;i<=n;i++)
fx[px[i].p]=i;
int l=1,r=mx+1;
while(l<=r)
{
int mid=(l+r)/2;
int sx=cale(mid);
if(sx<L)
l=mid+1;
else
r=mid-1;
}
printf("Case #%d: ",kt);
int ans=cale(l);
if(ans>=L)
printf("%d %d\n",l,ans);
else
printf("impossible\n");
}
}