【ZOJ】3229 Shoot the Bullet 上下界最大流

来源:互联网 发布:欧元国家知乎 编辑:程序博客网 时间:2024/04/30 00:09
Shoot the Bullet

Time Limit: 2 Seconds     Memory Limit:32768 KB      Special Judge

Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It is a utopia where humans and other beings such as fairies,youkai(phantoms), and gods live peacefully together. Shameimaru Aya is a crow tengu with the ability to manipulate wind who has been inGensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns theBunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautifuldanmaku(barrange) or cute girls living inGensokyo. She is the biggest connoisseur of rumors about the girls ofGensokyo among the tengu. Her intelligence gathering abilities are the best inGensokyo!

During the coming n days, Aya is planning to take many photos ofm cute girls living inGensokyo to write Bunbunmaru News daily and record at leastGx photos of girlx in total in the Bunkachou. At thek-th day, there areCk targets, Tk1,Tk2, ...,TkCk. The number of photos of targetTki that Aya takes should be in range [Lki,Rki], if less, Aya cannot write an interesting article, if more, the girl will become angry and use herlast spell card to attack Aya. What's more, Aya cannot take more thanDk photos at the k-th day. Under these constraints, the more photos, the better.

Aya is not good at solving this complex problem. So she comes to you, an earthling, for help.

Input

There are about 40 cases. Process to the end of file.

Each case begins with two integers 1 <= n <= 365, 1 <=m <= 1000. Thenm integers, G1, G2, ...,Gm in range [0, 10000]. Thenn days. Each day begins with two integer 1 <=C <= 100, 0 <= D <= 30000. Then C different targets. Each target is described by three integers, 0 <=T <m, 0 <= L <= R <= 100.

Output

For each case, first output the number of photos Aya can take,-1 if it's impossible to satisfy her needing. If there is a best strategy, output the number of photos of each girl Aya should take at each day on separate lines. The output must be in the same order as the input. If there are more than one best strategy, any one will be OK.

Output a blank line after each case.

Sample Input

2 312 12 123 180 3 91 3 92 3 93 180 3 91 3 92 3 92 312 12 123 180 3 91 3 92 3 93 180 0 31 3 62 6 92 312 12 123 150 3 91 3 92 3 93 210 0 31 3 62 6 12

Sample Output

3666666636963369-1

External Links

TeamShanghaiAlice(banner)
Wikipedia
Touhou Wiki


Author: WU, Zejun

Source: ZOJ Monthly, July 2009


传送门:【ZOJ】3229 Shoot the Bullet


题目大意:

有一个人,他要给m(1 <= m <= 1000)个女孩子拍照,每个女孩子 i 都需要至少拍G[ i ]张照。一共要拍n(1 <= n <= 365)天,每天要给C(1 <= C <= 100)个女孩子拍照,并且每天可以拍的照片不能超过D张(0 <= D <= 30000)。每天每个女孩子被拍照时都有一个范围[L,R],不能少于L也不能大于R。问是否能在符合条件的情况下拍下尽量多的照片。

如果不能满足要求,输出-1,否则输出最大的拍的照片数(总和)以及每天每个女孩子拍照的数量。


题目分析:

本题是网络流中的有上下界的最大流模型。

简单说一下怎么建图吧。

首先设in[ i ]表示进入i的下界之和减去从i出去的下界之和(in[ i ] < 0出的多于进的,反之亦然)然后建立源s汇t,对第i天,建边(s,i,x)表示第i天最多拍x张照。对第i天中的要求为[L,R]的第j个女孩纸建边(i,j+n,R-L),并且in[ i ] -= L,in[ j + n ] += L。接下来对每个女孩纸j建边(j+n,t,G[ j ])表示至少要拍G[ i ]张。

接下来!建立附加源汇ss,tt,对所有in[ i ] > 0的点建边(ss,i,in[ i ])表示给足需要流入的in[ i ]单位的下界,对所有in[ i ] < 0的点建边(i,tt,-in[ i ])表示提供出口释放需要流出的in[ i ]单位的下界。

然后跑一次ss->tt的最大流。

如果所有从ss出发的边满流

或者进入tt的边满流

或者是等于流量之和flow

或者s->t的反悔边的流量等于in[ i ] > 0的in[ i ]之和

则表示原图存在可行流。

然后将附加源汇删除,再跑一次s->t的最大流,此时的流量flow就是结果(flow = 第一次最大流后s->t的反悔边的流量 + 原图中剩余的自由流 ),输出结果的时候,每条边的流量就是第二次跑完最大流后的流量+分离出去的下界的容量。


代码如下:


#include <cstdio>#include <cstring>#include <algorithm>using namespace std ;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REPV( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define clear( a , x ) memset ( a , x , sizeof a )#define copy( a , x ) memcpy ( a , x , sizeof a )const int MAXN = 1000 + 365 + 10 ;const int MAXE = 90000 ;const int MAXQ = 90000 ;const int INF = 0x3f3f3f3f ;struct Edge {int v , c , n ;Edge ( int var = 0 , int capcity = 0 , int next = 0 ) :v ( var ) , c ( capcity ) , n ( next ) {}} ;struct netWork {Edge edge[MAXE] ;int adj[MAXN] , cntE ;int d[MAXN] , cur[MAXN] , num[MAXN] , pre[MAXN] ;bool vis[MAXN] ;int Q[MAXQ] ;int head , tail ;int nv ;int flow ;void init () {cntE = 0 ;clear ( adj , -1 ) ;}void addedge ( int u , int v , int c , int rc = 0 ) {edge[cntE] = Edge ( v ,  c , adj[u] ) ;adj[u] = cntE ++ ;edge[cntE] = Edge ( u , rc , adj[v] ) ;adj[v] = cntE ++ ;}void rev_Bfs ( int t ) {clear ( vis , 0 ) ;clear ( num , 0 ) ;head = tail = 0 ;Q[tail ++] = t ;num[0] = 1 ;d[t] = 0 ;vis[t] = true ;while ( head != tail ) {int u = Q[head ++] ;for ( int i = adj[u] ; ~i ; i = edge[i].n ) {int v = edge[i].v ;if ( !vis[v] ) {vis[v] = true ;d[v] = d[u] + 1 ;++ num[d[v]] ;Q[tail ++] = v ;}}}}int ISAP ( int s , int t ) {copy ( cur , adj ) ;rev_Bfs ( t ) ;flow = 0 ;int i , u = pre[s] = s ;while ( d[s] < nv ) {if ( u == t ) {int f = INF , pos ;for ( i = s ; i != t ; i = edge[cur[i]].v )if ( f > edge[cur[i]].c ) {f = edge[cur[i]].c ;pos = i ;}for ( i = s ; i != t ; i = edge[cur[i]].v ) {edge[cur[i]].c -= f ;edge[cur[i] ^ 1].c += f ;}flow += f ;u = pos ;}for ( i = cur[u] ; ~i ; i = edge[i].n )if ( edge[i].c && d[u] == d[edge[i].v] + 1 )break ;if ( ~i ) {cur[u] = i ;pre[edge[i].v] = u ;u = edge[i].v ;}else {if ( 0 == ( -- num[d[u]] ) )break ;int mmin = nv ;for ( i = adj[u] ; ~i ; i = edge[i].n )if ( edge[i].c && mmin > d[edge[i].v] ) {cur[u] = i ;mmin = d[edge[i].v] ;}d[u] = mmin + 1 ;++ num[d[u]] ;u = pre[u] ;}}return flow ;}} ;netWork net ;int n , m , e ;int in[MAXN] ;int bound[36500] ;int capcity[365] ;int s , t ;int ss , tt ;int read () {int x = 0 ;char c = ' ' ;while ( c < '0' || c > '9' ) c = getchar () ;while ( c >= '0' && c <= '9' ) x = x * 10 + c - '0' , c = getchar () ;return x ;}int build () {int dayphoto , girl ;int idx , b , c ;net.init () ;clear ( in , 0 ) ;e = 0 ;s = n + m ;t = n + m + 1 ;ss = n + m + 2 ;tt = n + m + 3 ;net.nv = n + m + 4 ;REP ( i , m ) {girl = read () ;in[i + n] -= girl ;in[t] += girl ;}REP ( i , n ) {dayphoto = read () , capcity[i] = read () ;while ( dayphoto -- ) {idx = read () , b = read () , c = read () ;in[i] -= b ;in[idx + n] += b ;bound[e ++] = b ;//记录去掉的下界的容量net.addedge ( i , idx + n , c - b ) ;//去掉下界的容量}}REP ( i , n )net.addedge ( s , i , capcity[i] ) ;//每天拍照的上限REP ( i , m )net.addedge ( i + n , t , INF ) ;//每个女孩子可以拍照的上限(根本没上限)int tot = 0 ;REP ( i , n + m + 4 ) {if ( in[i] > 0 ) {//进入i的下界和大于从i出去的下界和tot += in[i] ;net.addedge ( ss , i ,  in[i] ) ;}else if ( in[i] < 0 )//进入i的下界和小于从i出去的下界和net.addedge ( i , tt , -in[i] ) ;}net.addedge ( t , s , INF ) ;//添加t->s的边int flow = net.ISAP ( ss , tt ) ;//求可行流if ( flow != tot )//flow==tot则满流,反之不满流return 0 ;//不满流net.adj[ss] = net.adj[tt] = -1 ;//删除附加源汇return 1 ;//满流}void work () {while ( ~scanf ( "%d%d" , &n , &m ) ) {if ( build () ) {int flow = net.ISAP ( s , t ) ;//求最大流printf ( "%d\n" , flow ) ;REP ( i , e )printf ( "%d\n" , bound[i] + net.edge[i << 1 | 1].c ) ;}elseprintf ( "-1\n" ) ;printf ( "\n" ) ;}}int main () {work () ;return 0 ;}


0 0
原创粉丝点击