链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229

 

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 in Gensokyo for over 1000 years. She runs the Bunbunmaru News - a newspaper chock-full of rumors, and owns the Bunkachou - her record of interesting observations for Bunbunmaru News articles and pictures of beautiful danmaku(barrange) or cute girls living in Gensokyo. She is the biggest connoisseur of rumors about the girls of Gensokyo among the tengu. Her intelligence gathering abilities are the best in Gensokyo!  

During the coming n days, Aya is planning to take many photos of m cute girls living in Gensokyo to write Bunbunmaru News daily and record at least Gx photos of girl x in total in the Bunkachou. At the k-th day, there are Ck targets, Tk1, Tk2, ..., TkCk. The number of photos of target Tki 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 her last spell card to attack Aya. What's more, Aya cannot take more than Dk 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. Then m integers, G1, G2, ..., Gm in range [0, 10000]. Then n 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 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 3 9
1 3 9
2 3 9

2 3
12 12 12
3 18
0 3 9
1 3 9
2 3 9
3 18
0 0 3
1 3 6
2 6 9

2 3
12 12 12
3 15
0 3 9
1 3 9
2 3 9
3 21
0 0 3
1 3 6
2 6 12

Sample Output

36
6
6
6
6
6
6

36
9
6
3
3
6
9

-1


 

题意:

n天里给m个人拍照

每一个人各自至少要拍Gx张

然后n天内。每天给c个人拍照,当天最多拍d张。c个人 T代表编号,l代表当天给这人拍照的下限。r是上限。

 

不行输出-1,能够输出最多n天拍多少张照片,按T输入顺序,输出该人 改天拍了几张。

 

做法:

按有源有汇带下界的网络流模型建模就可以。

 

然后要查个流量来推断 该人该天拍了多少张照片,所以。我先建的是  每天和人之间的边。

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map> 

const int MAXN = 22222;//点数的最大值
const int MAXM = 882222;//边数的最大值
const int INF = 2000000000;
struct Edge
{
	int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void init()
{
	tol = 0;
	memset(head,-1,sizeof (head));
}
void addedge (int u,int v,int w,int rw = 0)//网络流要有反向弧
{
	edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
	edge[tol].next = head[u]; head[u] = tol++;
	edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
	edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
	memset(dep,-1,sizeof(dep));
	memset(gap,0,sizeof(gap));
	gap[0] = 1;
	int front = 0, rear = 0;
	dep[end] = 0;
	Q[rear++] = end;
	while(front != rear)
	{
		int u = Q[front++];
		for(int i = head[u]; i !=  -1; i = edge[i].next)
		{
			int v = edge[i]. to;
			if(dep[v] != -1)continue;
			Q[rear++] = v;
			dep[v] = dep[u] + 1;
			gap[dep[v]]++;
		}
	}
}
int S[MAXN];
int sap(int start,int end, int N)//有几个点
{
	BFS(start,end);
	memcpy(cur,head,sizeof(head));  
	int top = 0;
	int u = start;
	int ans = 0;
	int i;
	while(dep[start] < N)
	{
		if(u == end)
		{
			int Min = INF;
			int inser;
			for( i = 0;i < top;i++)
			{
				if(Min > edge[S[i]].cap - edge[S[i]].flow)
				{
					Min = edge[S[i]].cap - edge[S[i]].flow;
					inser = i;
				}
			}
			for( i = 0;i < top;i++)
			{
				edge[S[i]]. flow += Min;
				edge[S[i]^1].flow -= Min;
			}
			ans += Min;
			top = inser;
			u = edge[S[top]^1].to;
			continue;
		}
		bool flag =  false;
		int v;
		for( i = cur[u]; i != -1; i = edge[i]. next)
		{
			v = edge[i]. to;
			if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
			{
				flag =  true;
				cur[u] = i;
				break;
			}
		}
		if(flag)
		{
			S[top++] = cur[u];
			u = v;
			continue;
		}
		int Min = N;
		for( i = head[u]; i !=  -1; i = edge[i].next)
		{
			if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
			{
				Min = dep[edge[i].to];
				cur[u] = i;
			}
		}
		gap[dep[u]]--;
		if(!gap[dep[u]]) return ans;
		dep[u] = Min + 1;
		gap[dep[u]]++;
		if(u != start)u = edge[S[--top]^1].to;
	}
	return ans;
} 

int g[1010];//人的下限
int in[5000];//下限入多少
int ttt[400][110];
int ccc[400];
int ddd[400];
int lll[400][110];
int  main()
{
	int n,m;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		init();
		int T=n+m+3;
		int S=n+m+2;
		int s=0;
		int t=n+m+1;
		for(int i=1;i<=n;i++)
		{ 
			scanf("%d",&g[i]);  //下限 
		}
		memset(in,0,sizeof in);  
		for(int i=1;i<=m;i++)
		{
			int c,d;
			scanf("%d%d",&ccc[i],&ddd[i]); 
			for(int j=1;j<=ccc[i];j++)
			{
				int l,r;
				scanf("%d%d%d",&ttt[i][j],&l,&r);
				lll[i][j]=l;
				addedge( i,ttt[i][j]+m+1, r-l);
				in[i]-=l;
				in[ttt[i][j]+m+1]+=l;
			} 
		}
		for(int i=1;i<=n;i++)
		{ 
			addedge(i+m,t, INF-g[i]);
			in[i+m]-=g[i];
			in[t]+=g[i];
		}
		for(int i=1;i<=m;i++)
			addedge(0, i, ddd[i]);

		//0 点 小s    n+m+1  小t     n+m+2 大S   n+m+3 大T


	
		int sum=0;
		for(int i=0;i<=t;i++)
		{
			if(in[i]<0) 
				addedge(i,T,-in[i]);
			else 
			{
				sum+=in[i];
				addedge(S,i,in[i]);
			}
		}	
		
		addedge(t,s,INF);
		int ans=sap(S,T,T+1);
		if(ans!=sum)
		{
			//printf("ans %d\n",ans);
			puts("-1\n");//上下限无法满足
			continue;
		}  
		addedge(S,s,INF);
		addedge(t,T,INF);
		ans=sap(S,T,T+1);
		 
		printf("%d\n",ans);//sum 已经在第一次流了  所以不用 再减去 sum
		//但事实上 f=f'-sum
		int ji=0;
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=ccc[i];j++)
			{
				printf("%d\n",edge[ji].flow+lll[i][j]);
				ji+=2;
			}
		}
		puts(""); 
	}
	return 0;
}