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!
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=shoot_the_bullet_0.jpg)
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, ..., 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
题目是说aya要给少女拍照片,一共需要n天,共有m个少女,在n天中,对第i个少女至少要拍G张照片。在第t天中,只能拍D张照片,而且是特定的第ct个人拍,而且要求在l到r张之间。问能否完成任务,如果完成最多拍多少张。
- 处理有源汇有上下界最大流问题是:
- 1.构造附加网络
- 2.对ss、tt求最大流(ss、tt满流则有解)
- 3.若有解,对s、t求最大流
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define M 2014
#define N 1000010
#define inf 0x3f3f3f
using namespace std;
int head[M],gap[M],dis[M],pre[M],cur[M];
int to[N],next[N],flow[N],cap[N];
int ns,n,m,st,ed,num;
int in[M],down[N],id[N];
void init()
{
memset(head,0,sizeof(head));
memset(in,0,sizeof(in));
num=2;
}
void addedge(int u,int v,int c)
{
to[num]=v;cap[num]=c;flow[num]=0;next[num]=head[u];head[u]=num++;
to[num]=u;cap[num]=0;flow[num]=0;next[num]=head[v];head[v]=num++;
}
void bfs()
{
memset(dis,0x3f,sizeof(dis));
queue<int> que;
que.push(ed);
dis[ed]=0;
while(!que.empty())
{
int u=que.front();
que.pop();
++gap[dis[u]];
for(int p=head[u];p;p=next[p])
{
int &v=to[p];
if(cap[p^1]>flow[p^1]&dis[v]>n)
{
dis[v]=dis[u]+1;
que.push(v);
}
}
}
}
int Max_flow(int ss,int tt,int nn)
{
st=ss;
ed=tt;
n=nn;
int ans = 0, minFlow = inf, u;
for(int i=0;i<=n;i++)
{
cur[i]=head[i];
gap[i]=0;
}
u=pre[st]=st;
bfs();
while(dis[st]<n)
{
bool flag=false;
for(int &p=cur[u];p;p=next[p])
{
int &v=to[p];
if(cap[p]>flow[p]&&dis[u]==dis[v]+1)
{
flag=true;
minFlow=min(minFlow,cap[p]-flow[p]);
pre[v]=u;
u=v;
if(u==ed)
{
ans+=minFlow;
while(u!=st)
{
u=pre[u];
flow[cur[u]]+=minFlow;
flow[cur[u]^1]-=minFlow;
}
minFlow=inf;
}
break;
}
}
if(flag)continue;
int minDis=n-1;
for(int p=head[u];p;p=next[p])
{
int &v=to[p];
if(cap[p]>flow[p]&&dis[v]<minDis)
{
minDis=dis[v];
cur[u]=p;
}
}
if(--gap[dis[u]]==0)break;
++gap[dis[u]=minDis+1];
u=pre[u];
}
return ans;
}
int main()
{
while(scanf("%d%d",&ns,&m)!=EOF)
{
init();
int s=ns+m+1,t=s+1,ss=t+1,tt=ss+1,c;
for(int i=1;i<=m;i++)
{
scanf("%d",&c);
in[i]-=c;
in[t]+=c;
addedge(i,t,inf);
}
int cnt=0;
int a,b,low,high;
for(int i=1;i<=ns;i++)
{
scanf("%d%d",&a,&b);
addedge(s,i+m,b);
while(a--)
{
scanf("%d%d%d",&c,&low,&high);
in[i+m]-=low;
in[c+1]+=low;
down[++cnt]=low;
id[cnt]=num;
addedge(i+m,c+1,high-low);
}
}
int sum=0;
for(int i=1;i<=t;i++)
{
if(in[i]>0){sum+=in[i];addedge(ss,i,in[i]);}
if(in[i]<0)addedge(i,tt,-in[i]);
}
addedge(t,s,inf);
if(sum!=Max_flow(ss,tt,tt))
printf("-1\n");
else
{
printf("%d\n",Max_flow(s,t,tt));
for(int i=1;i<=cnt;i++)
printf("%d\n",down[i]+flow[id[i]]);
}
printf("\n");
}
return 0;
}