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个美女拍照,对于每个美女有一个限制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
#include
#include
#include
#include
#include
#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< 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] 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]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<