题目描述
现在,程设老师想要知道,最少花多少钱,能够在这n天中满足每天的需要呢?若无法满足,则请输出”impossible”。注意,由于程设老师良心大大的坏,所以他是可以不把濒死的研究生送去医院的!
输入
本题包含多组数据;第一行是一个数T(T<=11),表示数据组数,以下T组数据。
对于每一组数据,第一行三个数,n,m,k;
以下一行n个数,表示a[1]…a[n]
接着一行2m个数,表示l[1],p[1]…l[n],p[n]
接着一行2k个数,表示d[1],q[1]…d[n],q[n]
输出
对于每组数据以样例的格式输出一行,两个数分别表示第几组数据和最少钱数。
样例输入
2
3 2 1
10 20 30
40 90 15 100
1 5
3 2 1
10 20 30
40 90 15 100
2 5
样例输出
Case 1: 4650
Case 2: impossible
题解
费用流
本题和 bzoj1221 差不多。
具体建图方法:
将每个点拆成两个,分别为xi和yi。
S->xi,容量为ai,费用为0;yi->T,容量为ai,费用为0;D->yi,容量为ai(或inf同理),费用为0;xi->xi+1,容量为inf,费用为0。
对于每所大学j,S->D(辅助节点),容量为l[j],费用为p[j]。
对于每家医院k,xi->yi+d[k],,容量为inf,费用为q[k]。
然后跑最小费用最大流,满流则解为最小费用,不满流则无解。
#include <cstdio>
#include <cstring>
#include <queue>
#define N 10000
#define M 500000
#define inf 0x3f3f3f3f
using namespace std;
queue<int> q;
int head[N] , to[M] , val[M] , cost[M] , next[M] , cnt , s , d , t , dis[N] , from[N] , pre[N];
void add(int x , int y , int v , int c)
{
to[++cnt] = y , val[cnt] = v , cost[cnt] = c , next[cnt] = head[x] , head[x] = cnt;
to[++cnt] = x , val[cnt] = 0 , cost[cnt] = -c , next[cnt] = head[y] , head[y] = cnt;
}
bool spfa()
{
int x , i;
memset(from , -1 , sizeof(from));
memset(dis , 0x3f , sizeof(dis));
dis[s] = 0 , q.push(s);
while(!q.empty())
{
x = q.front() , q.pop();
for(i = head[x] ; i ; i = next[i])
if(val[i] && dis[to[i]] > dis[x] + cost[i])
dis[to[i]] = dis[x] + cost[i] , from[to[i]] = x , pre[to[i]] = i , q.push(to[i]);
}
return ~from[t];
}
int main()
{
int T , Case;
scanf("%d" , &T);
for(Case = 1 ; Case <= T ; Case ++ )
{
memset(head , 0 , sizeof(head)) , cnt = 1;
int n , m , k , i , x , y , f = 0 , ans = 0;
scanf("%d%d%d" , &n , &m , &k) , s = 0 , d = 2 * n + 1 , t = 2 * n + 2;
for(i = 1 ; i < n ; i ++ ) add(i , i + 1 , inf , 0);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &x) , add(s , i , x , 0) , add(i + n , t , x , 0) , add(d , i + n , inf , 0) , f += x;
for(i = 1 ; i <= m ; i ++ ) scanf("%d%d" , &x , &y) , add(s , d , x , y);
while(k -- )
{
scanf("%d%d" , &x , &y);
for(i = 1 ; i <= n - x - 1 ; i ++ ) add(i , i + x + 1 + n , inf , y);
}
while(spfa())
{
x = inf;
for(i = t ; i != s ; i = from[i]) x = min(x , val[pre[i]]);
f -= x , ans += x * dis[t];
for(i = t ; i != s ; i = from[i]) val[pre[i]] -= x , val[pre[i] ^ 1] += x;
}
printf("Case %d: " , Case);
if(f) printf("impossible\n");
else printf("%d\n" , ans);
}
return 0;
}