bzoj1221的变形版本,弄懂1221,这题还是很简单的
1 const inf=100000007; 2 type node=record 3 point,next,flow,cost:longint; 4 end; 5 6 var edge:array[0..500010] of node; 7 q:array[0..500010] of longint; 8 cur,pre,d,p,a:array[0..110] of longint; 9 v:array[0..110] of boolean; 10 i,j,x,y,e,n,m,k,t,te,len,sf:longint; 11 12 function min(a,b:longint):longint; 13 begin 14 if a>b then exit(b) else exit(a); 15 end; 16 17 procedure add(x,y,f,c:longint); 18 begin 19 inc(len); 20 edge[len].point:=y; 21 edge[len].flow:=f; 22 edge[len].cost:=c; 23 edge[len].next:=p[x]; 24 p[x]:=len; 25 end; 26 27 function spfa:boolean; 28 var f,r,i,x,y:longint; 29 begin 30 for i:=1 to t do 31 d[i]:=inf; 32 d[0]:=0; 33 f:=1; 34 r:=1; 35 fillchar(v,sizeof(v),false); 36 v[0]:=true; 37 while f<=r do 38 begin 39 x:=q[f]; 40 i:=p[x]; 41 v[x]:=false; 42 while i<>-1 do 43 begin 44 y:=edge[i].point; 45 if edge[i].flow>0 then 46 if d[y]>d[x]+edge[i].cost then 47 begin 48 d[y]:=d[x]+edge[i].cost; 49 pre[y]:=x; 50 cur[y]:=i; 51 if not v[y] then 52 begin 53 inc(r); 54 q[r]:=y; 55 v[y]:=true; 56 end; 57 end; 58 i:=edge[i].next; 59 end; 60 inc(f); 61 end; 62 if d[t]=inf then exit(false) else exit(true); 63 end; 64 65 procedure mincost; 66 var i,j,neck,f,c:longint; 67 begin 68 f:=0; 69 c:=0; 70 while spfa do 71 begin 72 neck:=inf; 73 i:=t; 74 while i<>0 do 75 begin 76 j:=cur[i]; 77 neck:=min(neck,edge[j].flow); 78 i:=pre[i]; 79 end; 80 i:=t; 81 while i<>0 do 82 begin 83 j:=cur[i]; 84 dec(edge[j].flow,neck); 85 inc(edge[j xor 1].flow,neck); 86 i:=pre[i]; 87 end; 88 f:=f+neck; 89 c:=c+d[t]*neck; 90 // writeln(f,' ',c); 91 end; 92 if f=sf then writeln(c) else writeln('impossible'); 93 end; 94 95 begin 96 readln(te); 97 for e:=1 to te do 98 begin 99 len:=-1; 100 fillchar(p,sizeof(p),255); 101 readln(n,m,k); 102 t:=n*2+1; 103 sf:=0; 104 for i:=1 to n do 105 begin 106 read(a[i]); 107 add(i,t,a[i],0); 108 add(t,i,0,0); 109 sf:=sf+a[i]; 110 end; 111 readln; 112 for i:=1 to m do 113 begin 114 read(x,y); 115 add(0,1,x,y); 116 add(1,0,0,-y); 117 end; 118 readln; 119 for i:=1 to k do 120 begin 121 read(x,y); 122 for j:=1 to n do 123 if j+1+x<=n then 124 begin 125 add(j+n,j+1+x,inf,y); 126 add(j+1+x,j+n,0,-y); 127 end 128 else break; 129 end; 130 for i:=1 to n do 131 begin 132 add(0,i+n,a[i],0); 133 add(i+n,0,0,0); 134 if i<>n then 135 begin 136 add(i,i+1,inf,0); //今天没有用的人可以明天用 137 add(i+1,i,0,0); 138 add(i+n,i+n+1,inf,0); 139 add(i+n+1,i+n,0,0); 140 end; 141 end; 142 write('Case ',e,': '); 143 mincost; 144 end; 145 end.