题解:
随便建一下图费用流就可以过吧。。。
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set> 10 #include<queue> 11 #include<string> 12 #define inf 1000000000 13 #define maxn 100000+5 14 #define maxm 100000+5 15 #define eps 1e-10 16 #define ll long long 17 #define pa pair<int,int> 18 #define for0(i,n) for(int i=0;i<=(n);i++) 19 #define for1(i,n) for(int i=1;i<=(n);i++) 20 #define for2(i,x,y) for(int i=(x);i<=(y);i++) 21 #define for3(i,x,y) for(int i=(x);i>=(y);i--) 22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go) 23 #define mod 1000000007 24 using namespace std; 25 inline int read() 26 { 27 int x=0,f=1;char ch=getchar(); 28 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 29 while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();} 30 return x*f; 31 } 32 int n,m,k,mincost,maxflow,tot=1,s,ss,t,sum,a[maxn],head[maxn],d[maxn],from[2*maxm]; 33 bool v[maxn]; 34 queue<int>q; 35 struct edge{int from,go,next,v,c;}e[2*maxm]; 36 void add(int x,int y,int z,int w) 37 { 38 e[++tot]=(edge){x,y,head[x],z,w};head[x]=tot; 39 e[++tot]=(edge){y,x,head[y],0,-w};head[y]=tot; 40 } 41 bool spfa() 42 { 43 for (int i=s;i<=t;i++){v[i]=0;d[i]=inf;} 44 q.push(s);d[s]=0;v[s]=1; 45 while(!q.empty()) 46 { 47 int x=q.front();q.pop();v[x]=0; 48 for (int i=head[x],y;i;i=e[i].next) 49 if(e[i].v&&d[x]+e[i].c<d[y=e[i].go]) 50 { 51 d[y]=d[x]+e[i].c;from[y]=i; 52 if(!v[y]){v[y]=1;q.push(y);} 53 } 54 } 55 return d[t]!=inf; 56 } 57 void mcf() 58 { 59 mincost=0;maxflow=0; 60 while(spfa()) 61 { 62 int tmp=inf; 63 for(int i=from[t];i;i=from[e[i].from]) tmp=min(tmp,e[i].v); 64 mincost+=d[t]*tmp;maxflow+=tmp; 65 for(int i=from[t];i;i=from[e[i].from]){e[i].v-=tmp;e[i^1].v+=tmp;} 66 } 67 } 68 int main() 69 { 70 freopen("input.txt","r",stdin); 71 freopen("output.txt","w",stdout); 72 int T=read(),cs=0; 73 while(T--) 74 { 75 n=read();m=read();k=read();sum=0; 76 s=0;ss=2*n+1;t=ss+1; 77 memset(head,0,sizeof(head));tot=1; 78 for1(i,n) 79 { 80 a[i]=read();sum+=a[i]; 81 add(s,i,a[i],0); 82 add(i+n,t,a[i],0); 83 } 84 for1(i,n-1)add(i,i+1,inf,0); 85 for1(i,m) 86 { 87 int x=read(),y=read(); 88 add(s,ss,x,y); 89 } 90 for1(i,n)add(ss,i+n,inf,0); 91 for1(i,k) 92 { 93 int x=read(),y=read(); 94 for1(j,n-x-1)add(j,j+x+1+n,inf,y); 95 } 96 mcf(); 97 printf("Case %d: ",++cs); 98 if(maxflow!=sum)printf("impossible\n");else printf("%d\n",mincost); 99 } 100 101 return 0; 102 }