ZOJ3229 Shoot the Bullet 上下界最大流

上下界的最大流,建图很简单,天数与源点连,容量为每天拍照上限。女孩与汇点连,容量为INF,在根据每个女孩每天能拍的范围,建图,在求一次上下界最大流。
试了一下DINIC,过了,注意点是要多输出一个换行。
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 in Gensokyo. 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 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

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

#define MAXN  20010
#define MAXM  1000010
#define INF 0xFFFFFF

struct edge
{
    int to,c,next;
};

edge e[MAXM];
int n,m; 
int que[MAXN*100],dis[MAXN],pre[MAXN],maxflow;
int head[MAXN],head2[MAXN],en,vn;
int st, ed;
int in[MAXN],low[MAXM]; 
int girl[1500],day[500];

void add(int a, int b, int c) 
{
    e[en].to=b;
    e[en].c=c;
    e[en].next=head[a];
    head[a]=en++;
    e[en].to=a;
    e[en].c=0;
    e[en].next=head[b];
    head[b]=en++;
}
 

bool bfs() 
{
	memset(dis,-1,sizeof(dis));
	que[0]=st,dis[st]=1;
	int t=1,f=0;
	while(f<t)
	{
		int j=que[f++];
		for(int k=head[j];k!=-1;k=e[k].next)
		{	
			int i=e[k].to;
			if(dis[i]==-1 && e[k].c)
			{
				que[t++]=i;
				dis[i]=dis[j]+1;
				if(i==ed) return true;
			}
		}
	}
	return false; 
} 

int update()  
{  
	int p,flow=INF;  
    for (int i=pre[ed];i!=-1;i=pre[i])
		if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;    
    for (int i=pre[ed];i!=-1;i=pre[i]) 
		e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;   
    maxflow+=flow; 
    return p;
}  

void dfs() 
{  
	memset(pre,-1,sizeof(pre));
	memcpy(head2,head,sizeof(head2));  
    for(int i=st,j;i!=-1;)  
    {  
        int flag=false;  
        for(int k=head[i];k!=-1;k=e[k].next)    
          if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )  
          {  
                pre[j]=i; 
				head2[i]=k;  
				i=j; 
				flag=true;  
                if(i==ed) 
					i=update();  
                if(flag) 
					break;  
          }  
        if (!flag) dis[i]=-1,i=pre[i];   
    }  
} 

int dinic()
{
	int ans=0;
	maxflow=0;
	while(bfs())
		dfs();
	return maxflow;
}

int limitflow()  
{ 
    int i,st0,ed0,en0,ret=0;
    st0=st,ed0=ed,en0=en;
    st=vn++,ed=vn++;  
    head[st]=head[ed]=-1;
    for(i=0;i<vn-2;++i)
    {
        if(in[i]>0)
			add(st,i,in[i]);
        if(in[i]<0)
			add(i,ed,-in[i]);
    }
    add(ed0,st0,INF);
    dinic();
    for(i=head[st];i!=-1;i=e[i].next)
        if(e[i].c)
			return -1;   
    st=st0,ed=ed0;       
    for(vn-=2,i=0;i<vn;++i)
        while(head[i]>=en0) 
			head[i]=e[head[i]].next; 
    dinic();
    for(i=head[st];i!=-1;i=e[i].next) 
		ret+=e[i^1].c;
    return ret;
}

void solve()
{
	st=0,ed=n+m+1,vn=n+m+2,en=0;
	memset(head,-1,sizeof(head));
	memset(in,0,sizeof(in));
	for(int i=1;i<=m;i++)
		scanf("%d",&girl[i]);
	int nn,no,L,R;
	int counts=0;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&nn,&day[i]);
		for(int j=1;j<=nn;j++)
		{
			scanf("%d%d%d",&no,&L,&R);
			no++;
			add(i,n+no,R-L);
			in[i]-=L;
			in[n+no]+=L;
			low[counts++]=L;
		}
	}
	for(int i=1;i<=m;i++)
	{
		add(i+n,ed,INF); 
		in[i+n]-=girl[i];
		in[ed]+=girl[i]; 
	}
	for(int i=1;i<=n;i++)
		add(st,i,day[i]); //只有上限 

	int ret=limitflow();
	if(ret==-1)
	{
		printf("-1\n\n");
		return;
	}
	printf("%d\n",ret);
	for(int i=0;i<counts;i++)
		printf("%d\n",e[(i*2)^1].c+low[i]);
	printf("\n");
}

int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
		solve();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值