bzoj3280: 小R的烦恼
同餐巾计划。
考前练练手。
// It is made by XZZ
#include<cstdio>
#include<algorithm>
#include<cstring>
#define il inline
#define rg register
#define vd void
#define sta static
typedef long long ll;
il int gi(){
rg int x=0,f=1;rg char ch=getchar();
while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
const int maxn=50+50+50+10,maxm=10000000,S=maxn-2,T=maxn-1;
int n,m,k,fir[maxn],dis[maxm],nxt[maxm],w[maxm],cost[maxm],id=1;
il vd link(int a,int b,int c,int d){
nxt[++id]=fir[a],fir[a]=id,dis[id]=b,w[id]=c,cost[id]=d;
nxt[++id]=fir[b],fir[b]=id,dis[id]=a,w[id]=0,cost[id]=-d;
}
int a[51],l[51],p[51],d[51],q[51],b[51];
il vd Mincost(int&Cost){
static int hd,tl,que[maxn*1000],dist[maxn],lst[maxn],inque[maxn];
start:
memset(dist,63,sizeof dist);
dist[S]=0;hd=tl=0;que[tl++]=S;inque[S]=1;
while(hd^tl){
int x=que[hd];
for(rg int i=fir[x];i;i=nxt[i]){
if(w[i]&&dist[dis[i]]>dist[x]+cost[i]){
dist[dis[i]]=dist[x]+cost[i];lst[dis[i]]=i;
if(!inque[dis[i]])inque[dis[i]]=1,que[tl++]=dis[i];
}
}
inque[x]=0;++hd;
}
if(dist[T]==dist[0])return;
int flow=2333333;
for(rg int i=lst[T];i;i=lst[dis[i^1]])flow=std::min(flow,w[i]);
for(rg int i=lst[T];i;i=lst[dis[i^1]])w[i]-=flow,w[i^1]+=flow,Cost+=flow*cost[i];
goto start;
}
il vd work(int Cs){
n=gi(),m=gi(),k=gi();
id=1;memset(fir,0,sizeof fir);
for(rg int i=1;i<=n;++i)a[i]=gi();
for(rg int i=1;i<=m;++i)l[i]=gi(),p[i]=gi();
for(rg int i=1;i<=k;++i)d[i]=gi(),q[i]=gi();
for(rg int i=1;i<=m;++i)link(S,i+100,l[i],p[i]);
for(rg int i=1;i<=n;++i)
for(rg int j=1;j<=m;++j)
link(j+100,i,2333333,0);
for(rg int i=1;i<=n;++i)link(S,i+50,a[i],0);
for(rg int i=1;i<=n;++i)b[i]=id+1,link(i,T,a[i],0);
for(rg int i=1;i<n;++i)link(i+50,i+51,2333333,0);
for(rg int i=1;i<=n;++i)
for(rg int j=1;j<=k;++j)
if(i+d[j]+1<=n)link(i+50,i+d[j]+1,2333333,q[j]);
int ans=0;
Mincost(ans);
for(rg int i=1;i<=n;++i)
if(w[b[i]]){printf("Case %d: impossible\n",Cs);return;}
printf("Case %d: %d\n",Cs,ans);
}
int main(){
#ifdef xzz
freopen("3280.in","r",stdin);
freopen("3280.out","w",stdout);
#endif
int T=gi();for(rg int i=1;i<=T;++i)work(i);
return 0;
}
博主是蒟蒻,有问题请指出,谢谢!
本博客中博文均为原创,未经博主允许请勿随意转载,谢谢。
本博客中博文均为原创,未经博主允许请勿随意转载,谢谢。