BZOJ 3280 费用流
思路:
同BZOJ 1221
//By SiriusRen
#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define N 666666
#define inf 0x3f3f3f3f
int cases,n,m,k,ed,cost[N],edge[N],next[N],first[6666],v[N],tot,a[666],l[666],p[666],d[666],q[666];
int vis[6666],with[6666],dis[6666],minn[6666],ans,all,All;
void Add(int x,int y,int C,int E){cost[tot]=C,edge[tot]=E,v[tot]=y,next[tot]=first[x],first[x]=tot++;}
void add(int x,int y,int C,int E){Add(x,y,C,E),Add(y,x,-C,0);}
bool tell(){
mem(vis,0),mem(with,0),mem(dis,0x3f),mem(minn,0x3f);
queue<int>q;q.push(0);dis[0]=0;
while(!q.empty()){
int t=q.front();q.pop(),vis[t]=0;
for(int i=first[t];~i;i=next[i])
if(dis[v[i]]>dis[t]+cost[i]&&edge[i]){
dis[v[i]]=dis[t]+cost[i],minn[v[i]]=min(minn[t],edge[i]),with[v[i]]=i;
if(!vis[v[i]])vis[v[i]]=1,q.push(v[i]);
}
}return dis[ed]!=inf;
}
int zeng(){
for(int i=ed;i;i=v[with[i]^1])
edge[with[i]]-=minn[ed],edge[with[i]^1]+=minn[ed];
All+=minn[ed];
return dis[ed]*minn[ed];
}
int main(){
scanf("%d",&cases);
for(int I=1;I<=cases;I++){
mem(first,-1),all=All=ans=tot=0;
scanf("%d%d%d",&n,&m,&k),ed=2*n+1;
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=m;i++)scanf("%d%d",&l[i],&p[i]);
for(int i=1;i<=k;i++)scanf("%d%d",&d[i],&q[i]);
for(int i=1;i<=n;i++)add(0,i,0,a[i]),add(n+i,ed,0,a[i]),all+=a[i];
for(int i=1;i<=m;i++)add(0,1+n,p[i],l[i]);
for(int i=1;i<n;i++){
add(n+i,n+i+1,0,inf);
for(int j=1;j<=k;j++)if(i+d[j]<n)
add(i,i+d[j]+n+1,q[j],inf);
}
while(tell())ans+=zeng();
printf("Case %d: ",I);
if(all==All)printf("%d\n",ans);
else puts("impossible");
}
}