题意:n天,m个学校,k个医院。每天需要a[i]个人,这a[i]个人工作后处于濒死状态,你可以选择把濒死状态的人送去医院,治好后还可以继续工作。每个医院i救治一个人需要d[i]天,费用q[i]元。学校 i 中一个人工作一天薪酬为p[ i ].
思路:
时间为一个天然序,第i天的决策为从健康的人中选择a[i]个人。并且这a[i]个人变为濒死的人。流量可以理解为决策的选择。
那么节点就需要设置为每天健康的人数和濒死的人数。节点状态确定后建图就很容易了
坑点:假设医院A救治一个人需要1天,第一天送去医院的人第三天才能使用,不是第二天。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf = 0x3f3f3f3f3f3f3f3f;
const LL maxn = 2000000;
const LL maxm = 2000000;
struct mcmf{
LL he[maxn],tot,edge[maxm],ne[maxm],ver[maxm],cost[maxm];
LL pre[maxn],d[maxn];
bool vis[maxn];
LL n;
void init(LL x){
n = x;
tot = 1;
for( LL i = 0;i <= n;i++ ) he[i] = 0;
}
void add(LL x,LL y,LL cap,LL c){
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
edge[tot] = cap;
cost[tot] = c;
ver[++tot] = x;
ne[tot] = he[y];
he[y] = tot;
edge[tot] = 0;
cost[tot] = -c;
}
bool spfa(LL s,LL t){
queue<LL>q;
for(LL i = 0;i <= n;i++){
d[i] = inf;
vis[i] = false;
pre[i] = -1;
}
d[s] = 0;
vis[s] = true;
q.push(s);
while(!q.empty()){
LL x = q.front();
q.pop();
vis[x] = false;
for(LL cure = he[x]; cure ;cure = ne[cure]){
LL y = ver[cure];
if(edge[cure] && d[y] > d[x] + cost[cure] ){
d[y] = d[x] + cost[cure];
pre[y] = cure;
if(!vis[y]){
vis[y] = true;
q.push(y);
}
}
}
}
if(pre[t] == -1)return false;
else return true;
}
LL mincost(LL s,LL t,LL &cc){
LL flow = 0;
cc = 0;
while(spfa(s,t)){
LL mn = inf;
for(LL cure = pre[t];cure != -1;cure = pre[ ver[cure^1] ]){
if(mn > edge[cure])
mn = edge[cure];
}
for(LL cure = pre[t];cure != -1;cure = pre[ ver[cure^1] ]){
edge[cure] -= mn;
edge[cure^1] += mn;
cc += cost[cure] * mn;
}
flow += mn;
}
return flow;
}
} g;
LL a[100],l[100],p[100],q[100],d[100];
int main()
{
LL T,sum,cost,n,m,k,ca= 0;
scanf("%lld",&T);
while( T-- ){
ca++;
scanf("%lld%lld%lld",&n,&m,&k);
sum = 0;cost = 0;
LL SS = 0, TT = 2*n+1;
g.init( TT + 10 );
for(LL i = 1; i <= n;i++){
scanf("%lld",&a[i]);
sum += a[i];
}
for( LL i = 1;i <= m;i++ ){
scanf("%lld%lld",&l[i],&p[i]);
}
for( LL i = 1;i <= k;i++ ){
scanf("%lld%lld",&d[i],&q[i]);
}
for( LL i = 1;i <= m;i++ ){
g.add( SS,1,l[i],p[i] );
}
for( LL i = 1;i < n;i++ ){
g.add( i,i+1,inf,0 );
g.add( i+n,i+n+1,inf,0 );
}
for (int i=1;i<=k;i++){
for (int j=1;j<=n-d[i]-1;j++) g.add(j+n,j+d[i]+1,inf,q[i]);
}
for( LL i = 1; i <= n;i++ ){
g.add( i,TT,a[i],0 );
g.add( SS,i+n,a[i],0 );
}
LL maxflow = g.mincost( SS,TT,cost );
printf("Case %lld: ",ca);
if( maxflow != sum ){
printf("impossible\n");
}else{
printf("%lld\n",cost);
}
}
return 0;
}