【ZOJ 3229】Shoot the Bullet

【题目】

题目传送门

Description

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 inGensokyo. 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 theBunkachou. At the k-th day, there are Ck targets, Tk1, Tk2, ..., Ck. 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 个人拍照,m 个人中第 i 个人至少要拍 Gi 张。

然后 n 天内,每天能给 C 个人拍照,当天最多拍 D 张。这 C 个人中拍照数量有限制,L 是下限,R 是上限。

如果不能满足要求,输出 -1;否则输出 n 天最多能拍多少照片,并且输出每天对 C 个人中每个人拍照的数量。

首先 S 向每天连 [ 0 , day ] 的边,每一天与要拍照的人连 [ l , r ] 的边,每个人和 T 连 [ Gi , inf ] 的边

对于有源和汇的上下界网络流,只要从 T 往 S 连一条 [ 0 , inf ] 的边,原图就成为一个无源点汇点的循环流图,新建节点 SS 和 TT。我们把某个点必须流入的流量减去必须流出的流量记为 x,若 x 大于 0 就从 SS 向它连一条 [ 0 , x ] 的边;否则就从它向 TT 连一条 [ 0 , -x ] 的边。这时求 SS 到 TT 的最大流就可以判断是否有解

判断有解之后再做一次 S 到 T 的最大流就可以得到答案了

【代码】

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 100000
#define M 1000000
#define inf 1ll<<31ll-1
using namespace std;
int n,m,s,t,ss,tt,num=1;
int r[M],v[M],w[M],next[M];
int d[N],f[N],g[N],ans[N],sum[N],first[N];
bool can[N];
void init()
{
	s=0,t=n+m+1;
	ss=t+1,tt=ss+1;
	memset(sum,0,sizeof(sum));
	memset(can,true,sizeof(can));
	memset(first,0,sizeof(first));
}
void add(int x,int y,int f)
{
	num++;
	next[num]=first[x];
	first[x]=num;
	v[num]=y;
	w[num]=f;
}
bool bfs(int start,int end)
{
	int x,y,i,j;
	memset(d,-1,sizeof(d));
	memcpy(f,first,sizeof(f));
	queue<int>q;
	q.push(start);
	d[start]=0;
	while(!q.empty())
	{
		x=q.front();
		q.pop();
		for(i=first[x];i;i=next[i])
		{
			y=v[i];
			if(w[i]&&d[y]==-1&&can[y])
			{
				d[y]=d[x]+1;
				if(y==end)
				  return true;
				q.push(y);
			}
		}
	}
	return false;
}
int dinic(int now,int end,int flow)
{
	if(now==end)
	  return flow;
	int x,delta,ans=0;
	for(int &i=f[now];i;i=next[i])
	{
		x=v[i];
		if(w[i]&&d[x]==d[now]+1&&can[x])
		{
			delta=dinic(x,end,min(flow,w[i]));
			w[i]-=delta;
			w[i^1]+=delta;
			flow-=delta;
			ans+=delta;
			if(!flow)  break;
		}
	}
	if(flow)  d[now]=-1;
	return ans;
}
int main()
{
	int c,d,i,j,T,L,R;
	while(~scanf("%d%d",&n,&m))
	{
		init();
		int k=0,maxflow=0;
		for(i=1;i<=m;++i)
		{
			scanf("%d",&g[i]);
			sum[n+i]+=g[i],sum[t]-=g[i];
			add(n+i,t,inf),add(t,n+i,0);
		}
		for(i=1;i<=n;++i)
		{
			scanf("%d%d",&c,&d);
			add(s,i,d),add(i,s,0);
			for(j=1;j<=c;++j)
			{
				scanf("%d%d%d",&T,&L,&R);
				r[num+1]=R,r[num+2]=R;
				sum[i]+=L,sum[n+T+1]-=L;
				add(i,n+T+1,R-L),add(n+T+1,i,0);
			}
		}
		add(t,s,inf),add(s,t,0);
		for(i=0;i<=n+m+1;++i)
		{
			if(sum[i]>0)  add(i,tt,sum[i]),add(tt,i,0),k+=sum[i];
			if(sum[i]<0)  add(ss,i,-sum[i]),add(i,ss,0);
		}
		while(bfs(ss,tt))
		  maxflow+=dinic(ss,tt,inf);
		if(maxflow!=k)
		{
			printf("-1\n\n");
			continue;
		}
		maxflow=0;
		can[ss]=false,can[tt]=false;
		while(bfs(s,t))
		  maxflow+=dinic(s,t,inf);
		printf("%d\n",maxflow);
		for(i=1;i<=n;++i)
		{
			int total=0;
			for(j=first[i];j;j=next[j])
			  if(v[j]!=s&&v[j]!=t&&v[j]!=ss&&v[j]!=tt)
			    ans[++total]=r[j]-w[j];
			for(j=total;j;--j)
			  printf("%d\n",ans[j]);
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/forever_dreams/article/details/81868664
ZOJ