zoj 3229(有上下界的最大流)

来源:互联网 发布:java的标识符和关键字 编辑:程序博客网 时间:2024/05/16 13:48
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 beautifuldanmaku(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 ndays. 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 312 12 123 180 3 91 3 92 3 93 180 3 91 3 92 3 92 312 12 123 180 3 91 3 92 3 93 180 0 31 3 62 6 92 312 12 123 150 3 91 3 92 3 93 210 0 31 3 62 6 12

Sample Output

3666666636963369-1

External Links

TeamShanghaiAlice(banner) 
Wikipedia
Touhou Wiki


Author: WU, Zejun

Source: ZOJ Monthly, July 2009

题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229

分析:这题比较明显的构图,把每一天看成一个点,每个女孩也看成一个点,增加源和汇,源向每一天连上[0,d]的边,每一天与每个女孩如果有拍照任务的话连上[l,c]的边,每个女孩与汇连上[g,oo]的边,于是构成一个有上下界的图,直接用上下界最大流来搞就行了,再次完美1A,今天手感不错~~~

代码:

#include<cstdio>#define min(a,b) (a<b?a:b)using namespace std;const int mm=888888;const int mn=2222;const int oo=1000000000;int node,src,dest,edge;int ver[mm],flow[mm],low[mm],next[mm];int head[mn],work[mn],dis[mn],q[mn],in[mn],g[mn],d[mn];inline void prepare(int _node,int _src,int _dest){    node=_node,src=_src,dest=_dest;    for(int i=0;i<node;++i)head[i]=-1,in[i]=0;    edge=0;}inline void addedge(int u,int v,int c){    ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++;    ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){    int i,u,v,l,r=0;    for(i=0;i<node;++i)dis[i]=-1;    dis[q[r++]=src]=0;    for(l=0;l<r;++l)        for(i=head[u=q[l]];i>=0;i=next[i])            if(flow[i]&&dis[v=ver[i]]<0)            {                dis[q[r++]=v]=dis[u]+1;                if(v==dest)return 1;            }    return 0;}int Dinic_dfs(int u,int exp){    if(u==dest)return exp;    for(int &i=work[u],v,tmp;i>=0;i=next[i])        if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0)        {            flow[i]-=tmp;            flow[i^1]+=tmp;            return tmp;        }    return 0;}void Dinic_flow(){    while(Dinic_bfs())    {        for(int i=0;i<node;++i)work[i]=head[i];        while(Dinic_dfs(src,oo));    }}int Limit_flow(){    int i,src0,dest0,edge0,ret=0;    src0=src,dest0=dest,edge0=edge;    src=node++,dest=node++;    head[src]=head[dest]=-1;    for(i=0;i<node-2;++i)    {        if(in[i]>0)addedge(src,i,in[i]);        if(in[i]<0)addedge(i,dest,-in[i]);    }    addedge(dest0,src0,oo);    Dinic_flow();    for(i=head[src];i>=0;i=next[i])        if(flow[i])return -1;    src=src0,dest=dest0;    for(node-=2,i=0;i<node;++i)        while(head[i]>=edge0)head[i]=next[head[i]];    Dinic_flow();    for(i=head[src];i>=0;i=next[i])ret+=flow[i^1];    return ret;}int main(){    int i,v,c,k,e,n,m,ans;    while(scanf("%d%d",&n,&m)!=-1)    {        prepare(n+m+2,0,n+m+1);        for(i=1;i<=m;++i)scanf("%d",&g[i]),in[i+n]-=g[i],in[dest]+=g[i];        for(e=0,i=1;i<=n;++i)        {            scanf("%d%d",&k,&d[i]);            while(k--)            {                scanf("%d%d%d",&v,&low[++e],&c);                v+=n+1,in[i]-=low[e],in[v]+=low[e];                addedge(i,v,c-low[e]);            }        }        for(i=1;i<=n;++i)addedge(src,i,d[i]);        for(i=1;i<=m;++i)addedge(i+n,dest,oo);        if((ans=Limit_flow())>=0)        {            printf("%d\n",ans);            for(i=1;i<=e;++i)printf("%d\n",flow[(i<<1)-1]+low[i]);        }        else printf("-1\n");        puts("");    }    return 0;}


原创粉丝点击