zoj 3229 dinic算法的非递归实现以及有上下界的有源汇的网络流的最大流的求解
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
题意:一个人给m个女孩照相,共进行n天,第一行输入的就是n,m。接下来一行m个数字,表示这m个女孩每个人要求最少照的相片数目。在接下来n组数据,每组数据第一行输入两个数c,d。其中c表示每天最多给多少个人照相,d表示每天最多照多少张相,然后c行数据,每行输入三个数,第一个数表示给第几个人照相,人从0开始计数,接下来两个数分别表示给那个人照相的上下限。
题解:赤裸裸的有上下界的最大流,但是这题的坑点在于卡时间,递归的dinic算法必挂,必须要用非递归的dinic算法。
ac代码:
#include<iostream> #include<cstring> #include<cstdio> #include<climits> #define MAXN 150500 using namespace std; struct Edge { int s,t,f,next; }edge[MAXN]; int stack[MAXN]; int n,m; int ent; int s,t,supers,supert; int girl; int d[1100]; int pre[1400]; int head[1400]; int cur[1400]; int flows[1400]; int Minn[370][1050]; int q[1400]; int Min(int a,int b) { return a<b?a:b; } void add(int start,int last,int flow) { edge[ent].s=start;edge[ent].t=last;edge[ent].f=flow;edge[ent].next=head[start];head[start]=ent++; edge[ent].s=last;edge[ent].t=start;edge[ent].f=0;edge[ent].next=head[last];head[last]=ent++; } bool bfs(int S,int T) { int temp; memset(pre,-1,sizeof(pre)); int hd=0,tail=0,i; q[tail++]=S; pre[S]=0; while(hd<tail) { temp=q[hd++]; hd%=1400; for(int i=head[temp];i!=-1;i=edge[i].next) { int temp2=edge[i].t; if(pre[temp2]==-1&&edge[i].f) { pre[temp2]=pre[temp]+1; q[tail++]=temp2; tail%=1400; if(temp2==T)return true; } } } return false; } int dinic(int start,int last) { int flow=0,now; while(bfs(start,last)) { int top=0; memcpy(cur,head,sizeof(head)); int u=start; while(1) { if(u==last)//如果找到终点结束对中间路径进行处理并计算出该流 { int minn=INT_MAX; for(int i=0;i<top;i++) { if(minn>edge[stack[i]].f) { minn=edge[stack[i]].f; now=i; } } flow+=minn; for(int i=0;i<top;i++) { edge[stack[i]].f-=minn; edge[stack[i]^1].f+=minn; } top=now; u=edge[stack[top]].s; } for(int i=cur[u];i!=-1;cur[u]=i=edge[i].next)//找出从u点出发能到的边 if(edge[i].f&&pre[edge[i].t]==pre[u]+1) break; if(cur[u]==-1)//如果从该点未找到可行边,将该点标记并回溯 { if(top==0)break; pre[u]=-1; u=edge[stack[--top]].s; } else//如果找到了继续运行 { stack[top++]=cur[u]; u=edge[cur[u]].t; } } } return flow; } int main() { int c,g,down,up; while(~scanf("%d%d",&n,&m)) { s=0;t=m+n+1; ent=0; supers=t+1;supert=t+2; memset(head,-1,sizeof(head)); memset(flows,0,sizeof(flows)); for(int i=1;i<=m;i++) { scanf("%d",&girl); flows[i+n]-=girl; flows[t]+=girl; add(i+n,t,INT_MAX); } int sign=ent; for(int i=1;i<=n;i++) { scanf("%d%d",&c,&d[i]); for(int j=1;j<=c;j++) { scanf("%d%d%d",&g,&down,&up); g++; Minn[i][g]=down;//纪录流的下界 flows[i]-=down; flows[g+n]+=down; add(i,g+n,up-down); } } int sign1=ent; for(int i=1;i<=n;i++)add(s,i,d[i]); add(t,s,INT_MAX); int temp=ent-1; int sum=0; for(int i=s;i<=t;i++) { if(flows[i]>0){add(supers,i,flows[i]);sum+=flows[i];} else if(flows[i]<0)add(i,supert,-flows[i]); } int flow1=dinic(supers,supert);//对构造好了的网络求最大流 if(flow1!=sum)//观察附加边是否满流,如果没有满流说明无可行流,输出-1,终止本次任务 printf("-1\n"); else//如果满流,去掉附加边再求一次最大流 { for(int i=head[supers];i!=-1;i=edge[i].next) { edge[i].f=0;edge[i^1].f=0; } for(int i=head[supert];i!=-1;i=edge[i].next)//去掉附加边的过程 { edge[i].f=0;edge[i^1].f=0; } edge[temp].f=0;edge[temp^1].f=0; flow1+=dinic(s,t);//对剩余网络求最大流 printf("%d\n",flow1); for(int i=sign;i<sign1;i+=2) { printf("%d\n",Minn[edge[i].s][edge[i].t-n]+edge[i^1].f);//输出路径 } } printf("\n"); } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。