您的位置:首页 > 其它

【bzoj3280】小R的烦恼 费用流

2017-06-13 08:56 323 查看
题目描述

小R最近遇上了大麻烦,他的程序设计挂科了。于是他只好找程设老师求情。善良的程设老师答应不挂他,但是要求小R帮助他一起解决一个难题。
问题是这样的,程设老师最近要进行一项邪恶的实验来证明P=NP,这个实验一共持续n天,第i天需要a[i]个研究生来给他搬砖。研究生毕竟也是人,所以雇佣研究生是需要钱的,机智的程设老师已经联系好了m所大学,第j所大学共有l[j]个研究生,同时雇佣这所大学的一个研究生需要p[j]元钱。
本来程设老师满心欢喜的以为,这样捡最便宜的max{a[i]}个研究生雇来,就可以完成实验;结果没想到,由于他要求硕士生们每天工作25个小时不许吃饭睡觉上厕所喝水说话咳嗽打喷嚏呼吸空气,因此一天下来给他搬砖的所有研究生都会进入濒死状态。濒死状态的研究生,毫无疑问,就不能再进行工作了。但是机智的老师早早联系好了k家医院,第i家医院医治一个濒死的研究生需要d[i]天,并且需要q[i]元钱。
现在,程设老师想要知道,最少花多少钱,能够在这n天中满足每天的需要呢?若无法满足,则请输出”impossible”。注意,由于程设老师良心大大的坏,所以他是可以不把濒死的研究生送去医院的!

输入

本题包含多组数据;第一行是一个数T(T<=11),表示数据组数,以下T组数据。
对于每一组数据,第一行三个数,n,m,k;
以下一行n个数,表示a[1]…a

接着一行2m个数,表示l[1],p[1]…l
,p

接着一行2k个数,表示d[1],q[1]…d
,q

输出

对于每组数据以样例的格式输出一行,两个数分别表示第几组数据和最少钱数。

样例输入

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
, to[M] , val[M] , cost[M] , next[M] , cnt , s , d , t , dis
, from
, pre
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: