ZOJ 3229 Shoot the Bullet

来源:互联网 发布:linux赋予用户组权限 编辑:程序博客网 时间:2024/05/29 21:16

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


【分析】
题目名字..射一发?

好像比较裸
根据题意把时间放在左边,妹子放在右边。根据限制连一波
跑有上下界网络流


【代码】

//zoj dinic#include<queue>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define inf 1e9+7#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;queue <int> q;const int mxn=1505;int dis[mxn],du[mxn],head[mxn],D[mxn],tmp[100005];int n,m,s,t,S,T,G,C,ans,tot,cnt,pos,neg,L,R,lastcnt,nw;struct edge {int to,next,flow;} f[100005];inline void add(int u,int v,int low,int high){    du[u]+=low,du[v]-=low;    f[++cnt].to=v,f[cnt].next=head[u],f[cnt].flow=high-low,head[u]=cnt;    f[++cnt].to=u,f[cnt].next=head[v],f[cnt].flow=0,head[v]=cnt;}inline bool bfs(){    memset(dis,-1,sizeof dis);    q.push(S),dis[S]=0;    while(!q.empty())    {        int u=q.front();q.pop();        for(int i=head[u];i;i=f[i].next)        {            int v=f[i].to,flow=f[i].flow;            if(flow>0 && dis[v]==-1)              dis[v]=dis[u]+1,q.push(v);        }    }    return dis[T]>0;}inline int find(int u,int low){    int i,j,a=0,sum=0;    if(u==T) return low;    for(i=head[u];i;i=f[i].next)    {        int v=f[i].to,flow=f[i].flow;        if(flow>0 && dis[v]==dis[u]+1 && (a=find(v,min(flow,low-sum))))        {            sum+=a;            f[i].flow-=a;            if(i&1) f[i+1].flow+=a;            else f[i-1].flow+=a;            if(sum==low) return sum;        }    }       if(!sum) dis[u]=-1;    return sum;}int main(){    int i,j,x;    while(scanf("%d%d",&n,&m)!=EOF)    {        tot=ans=pos=neg=cnt=0,M(head),M(du);        s=0,t=n+m+1,S=t+1,T=t+2;        fo(i,1,m)          scanf("%d",&G),add(i+n,t,G,inf);        fo(i,1,n)        {            scanf("%d%d",&C,&D[i]);            while(C--)            {                scanf("%d%d%d",&x,&L,&R);                add(i,n+x+1,L,R);tmp[++tot]=L;            }        }lastcnt=cnt,tot=0;        fo(i,1,n) add(s,i,0,D[i]);        add(t,s,0,inf);        fo(i,s,t)          if(du[i]<0) add(S,i,0,-du[i]),neg-=du[i];          else if(du[i]>0) add(i,T,0,du[i]),pos+=du[i];        if(neg!=pos) {puts("-1");continue;}        while(bfs()) ans+=find(S,inf);        if(ans!=pos) {puts("-1");continue;}        S=s,T=t,ans=0,add(T,S,0,-inf);        while(bfs()) ans+=find(S,inf);        printf("%d\n",ans);        for(i=m+m+2;i<=lastcnt;i+=2)        {            printf("%d\n",f[i].flow+tmp[++tot]);        }        puts("");    }    return 0;}
阅读全文
0 0