有源汇上下界最大流 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

External Links

TeamShanghaiAlice(banner) 
Wikipedia
Touhou Wiki


ZOJ应该是评测不了

姑且认为代码是对的


可行流看这里


有源汇上下界最大流

先做可行流,再做最大流

具体讲一讲

因为已经有源汇不满足流量平衡

T->S连一条inf

然后直接套可行流就有可行流了

之后删掉新源汇、T->S的inf

再跑一遍最大流就完了


#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<set>
#include<map>
using namespace std;

typedef long long ll;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)x=-x,putchar('-');if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=1510,M=100100,inf=0X3f3f3f3f;

int m,n,S=N-2,T=N-1;

int ecnt=1,last[N];
struct EDGE{int to,nt,val;}e[M];
inline void readd(int u,int v,int val)
{e[++ecnt]=(EDGE){v,last[u],val};last[u]=ecnt;}
inline void add(int u,int v,int val)
{readd(u,v,val);readd(v,u,0);}//cout<<u<<" "<<v<<" "<<val<<endl;

int in[N],low[M],day[N];

void initial()
{ecnt=1;memset(last,0,sizeof(last));memset(in,0,sizeof(in));}

int d[N],q[N];

bool bfs(int s,int t)
{
	memset(d,0,sizeof(d));memset(q,0,sizeof(q));
	register int i,u,head=0,tail=1;
	q[0]=s;d[s]=1;
	while(head<tail)
	{
		u=q[head++];
		for(i=last[u];i;i=e[i].nt)if(e[i].val&&!d[e[i].to])
		{
			d[e[i].to]=d[u]+1;
			q[tail++]=e[i].to;
		}
	}
	return d[t];
}

int dfs(int u,int lim,int aim)
{
	if(aim==u||!lim)return lim;
	int res=0,tmp;
	for(int i=last[u];i;i=e[i].nt)if(e[i].val&&d[e[i].to]==d[u]+1)
	{
		tmp=dfs(e[i].to,min(lim,e[i].val),aim);
		e[i].val-=tmp;e[i^1].val+=tmp;res+=tmp;lim-=tmp;
		if(!tmp)d[e[i].to]=-1;if(!lim)break;
	}
	return res;
}

void dinic(int s,int t)
{while(bfs(s,t))dfs(s,inf,t);}

inline bool check()
{
	int SS=N-4,TT=N-3;
	register int i;
	for(i=1;i<=n+m;++i)in[i]>0?add(SS,i,in[i]):add(i,TT,-in[i]);
	in[S]>0?add(SS,S,in[S]):add(S,TT,-in[S]);//cout<<"DFS"<<endl;
	in[T]>0?add(SS,S,in[T]):add(S,TT,-in[T]);
	dinic(SS,TT);
	for(i=last[SS];i;i=e[i].nt)if(e[i].val)return 0;
	return 1;
}

int main()
{
	register int i,j,x,val,ed,num,ans;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		initial();ans=0;
		for(i=1;i<=m;++i)
		{val=read();in[T]+=val;in[i+n]-=val;}
		ed=0;
		for(i=1;i<=n;++i)
		{
			x=read();day[i]=read();
			for(j=1;j<=x;++j)
			{
				num=read();
				low[++ed]=read();val=read();
				add(i,n+num+1,val-low[ed]);
				in[i]-=low[ed];in[n+num+1]+=low[ed];
			}
		}
		for(i=1;i<=n;++i)add(S,i,day[i]);
		for(i=1;i<=m;++i)add(i+n,T,inf);
		add(T,S,inf);
		if(check())
		{
			dinic(S,T);add(T,S,-inf);
			for(i=last[S];i;i=e[i].nt)ans+=e[i^1].val;
			print(ans);puts("");
			for(i=1;i<=ed;++i)print(e[(i<<1)^1].val+low[i]),puts("");
		}
		else puts("-1");
		puts("");
	}
}
/*
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

36
6
6
6
6
6
6

36
9
6
3
3
6
9

-1
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值