zoj 3229 Shoot the Bullet (有源汇有上下界的最大流)

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, Tk1Tk2, ..., TkCk. The number of photos of target Tki that Aya takes should be in range [LkiRki], 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, G1G2, ..., 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个美女拍照,对于每个美女有一个限制g[i],表示n天拍照的总数>=g[i]。对于每天给出这一天需要拍照的美女数量c,和这天拍照数量的上限d,接下来c行每行给出三个数 k,l,r, k表示美女的编号,l,r表示拍照数量的上下界。

题解:有源汇有上下界的最大流

要求最大流,先求可行流,通过“有源汇网络的可行流”的求解方法来判断有源汇网络存在可行流。 

下文中ss表示附加源点,tt表示附加汇点,S表示原图的源点,T表示原图的汇点
若存在可行流,记从ss流出的流量sum1.然后将T->S的边取消,再次从S到T求解网络的最大流,记从S流出的流量sum2. 那么该有源汇网络的最大流为 sum1 + sum2. 
其中,sum1是在网络满足流量下界的条件下,一组可行解对应的流量值;求出sum1之后,网络中可能还有余量可以继续增广,那么再次求解从S到T的最大流,得到sum2,sum1 + sum2即为最终的最大流。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define N 1000003
#define M 2000
#define inf 1000000000
using namespace std;
int n,m,tot;
int point[M],next[N],v[N],remain[N],last[M],deep[M],num[M],cur[M];
int in[M],out[M],mark[1003][403],girl[M],d[M],cal[M],low[1003][403],sum[M];
void init()
{
	tot=-1;
	memset(point,-1,sizeof(point));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	memset(num,0,sizeof(num));
	memset(remain,0,sizeof(remain));
}
void add(int x,int y,int z)
{
	tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z;
	tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
	//cout<<x<<" "<<y<<" "<<z<<endl;
}
/*int addflow(int s,int t)
{
	int ans=inf; int now=t;
	while (now!=s) {
		ans=min(ans,remain[last[now]]);
		now=v[last[now]^1];
	}
	now=t;
	while (now!=s) {
		remain[last[now]]-=ans;
		remain[last[now]^1]+=ans;
		now=v[last[now]^1];
	}
	return ans;
}
void bfs(int s,int t)
{
	for (int i=1;i<=t;i++) deep[i]=t;
	queue<int> p; deep[t]=0; p.push(t);
	while (!p.empty()){
		int x=p.front(); p.pop();
		for (int i=point[x];i!=-1;i=next[i])
		 if (remain[i^1]&&deep[v[i]]==t) 
		  deep[v[i]]=deep[x]+1,p.push(v[i]);
	}
}
int isap(int s,int t)
{
	bfs(s,t);
	for (int i=1;i<=t;i++) num[deep[i]]++;
	for (int i=1;i<=t;i++) cur[i]=point[i];
    int now=s; int ans=0;
    while (deep[s]<t) {
    	if (now==t) {
    		ans+=addflow(s,t);
    		now=s;
		}
		bool pd=false;
		for (int i=cur[now];i!=-1;i=next[i])
		 if (deep[now]==deep[v[i]]+1&&remain[i]) {
		 	last[v[i]]=i;
		 	cur[now]=i;
		 	pd=true;
		 	now=v[i];
		 	break;
		 }
		if (!pd) {
			int minn=t;
			for (int i=point[now];i!=-1;i=next[i])
			 if (remain[i]) minn=min(minn,deep[v[i]]);
			if (!--num[deep[now]]) break;
			deep[now]=minn+1;
			num[deep[now]]++;
			cur[now]=point[now];
			if (now!=s) now=v[last[now]^1];
		}
	}
	return ans;
}*/
int addflow(int s,int t)
{
	int ans=inf; int now=t;
	while (now!=s) {
		ans=min(ans,remain[last[now]]);
		now=v[last[now]^1];
	}
	now=t;
	while (now!=s) {
		remain[last[now]]-=ans;
		remain[last[now]^1]+=ans;
		now=v[last[now]^1];
	}
	return ans;
}
void bfs(int s,int t)
{
	for (int i=1;i<=t;i++) deep[i]=t;
	deep[t]=0;
	queue<int> p;
	p.push(t);
	while (!p.empty()){
		int x=p.front(); p.pop();
		for (int i=point[x];i!=-1;i=next[i])
		 if (deep[v[i]]==t&&remain[i^1])
		  deep[v[i]]=deep[x]+1,p.push(v[i]);
	}
}
int isap(int s,int t)
{
	bfs(s,t); int ans=0;
	for (int i=1;i<=t;i++) num[deep[i]]++;
	for (int i=1;i<=t;i++) cur[i]=point[i];
	int now=s;
	while (deep[s]<t) {
		if (now==t) {
			ans+=addflow(s,t);
			now=s;
		}
		bool pd=false;
		for (int i=cur[now];i!=-1;i=next[i])
		 if (remain[i]&&deep[v[i]]+1==deep[now]){
		 	cur[now]=i;
		 	last[v[i]]=i;
		 	now=v[i];
		 	pd=true;
		 	break;
		 }
		if (!pd) {
			int minn=t;
			for (int i=point[now];i!=-1;i=next[i])
			 if (remain[i]) minn=min(minn,deep[v[i]]);
			if (!--num[deep[now]]) break;
			deep[now]=minn+1;
			num[deep[now]]++;
			cur[now]=point[now];
			if (now!=s) {
				now=v[last[now]^1];
			}
 		}
	}
	return ans;
}
bool check()
{
	for (int i=2;i<=n+m+1;i++)
	  if (abs(sum[i])!=remain[cal[i]]) return false;
	return true;
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	while (scanf("%d%d",&n,&m)!=EOF){
		init();
		for (int i=1;i<=m;i++) scanf("%d",&girl[i]);
		int s=1; int t=n+m+2;
		for (int i=1;i<=n;i++) {
			int c;
			scanf("%d%d",&c,&d[i]);
			add(s,i+1,d[i]); out[s]+=d[i];
			for (int j=1;j<=c;j++){
				int x,l,r;
				scanf("%d%d%d",&x,&l,&r); x++;
				add(i+1,x+n+1,r-l);
				in[x+n+1]+=l; out[i+1]+=l; 
				mark[x][i]=tot; low[x][i]=l;
			}
		}
		for (int i=1;i<=m;i++) add(i+n+1,t,inf-girl[i]),out[i+n+1]+=girl[i],in[t]+=girl[i];
		int ss=t+1; int tt=ss+1;
		int size=0;
		for (int i=1;i<=n;i++) {
			sum[i+1]=out[i+1]-in[i+1];
			if (sum[i+1]>0) size+=sum[i+1],add(i+1,tt,sum[i+1]);
			else add(ss,i+1,-sum[i+1]);
			cal[i+1]=tot;
		}
		for (int i=1;i<=m;i++) {
			sum[i+n+1]=out[i+n+1]-in[i+n+1];
			if (sum[i+1+n]>0) size+=sum[i+n+1],add(i+n+1,tt,sum[i+1+n]);
			else add(ss,i+1+n,-sum[i+1+n]);
			cal[i+n+1]=tot;
		}
		int chan=0,sums,sumt;
		add(t,s,inf); chan=tot;
		add(s,tt,0); sums=tot; add(ss,t,in[t]); sumt=tot;
		int sum1=isap(ss,tt);
		//cout<<sum1<<endl;
		//for (int i=0;i<tot;i++) cout<<remain[i]<<" ";
		//cout<<endl;
		//cout<<size<<" "<<sum1<<endl;
		if (!check()||remain[sums]!=0||remain[sumt]!=in[t]){
			printf("-1\n");
			printf("\n");
			continue;
		}
		remain[chan]=0; remain[chan^1]=0;
		int sum2=isap(s,t);
		printf("%d\n",sum1+sum2);
		for (int i=1;i<=m;i++) {
			for (int j=1;j<=n;j++)
			 printf("%d\n",remain[mark[i][j]]+low[i][j]);
		}
		printf("\n");
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值