Describe
http://cojs.tk/cogs/problem/problem.php?pid=396
The ball that is numbered consecutively starting from 1, is placed on n pillars according to I\, and the necessary condition for \ (j\) is \ (i+j\) is a complete square number. Ask how many balls you can put on.
Analysis
Cogs is a simplified version, I found a full version of the test data on the Internet, require the output solution ...
It is inconvenient to ask for the maximum number of balls, and we consider enumerating the largest balloons and calculating the minimum number of pillars required.
We have an edge for satisfying \ (j<i\) and \ (i+j\) as a complete squared (i,j\), from \ (j\) to \ (i\). Then we'll use a few paths to connect all the dots to a few strings. This path is equivalent to a pillar. So this is the problem of finding the minimum path coverage.
Attention!!! is the smallest path overlay! is not the minimum edge overlay!!!
We split each point \ (i\) into \ (i\) and \ (i ' \) to create a binary graph, and if \ (i,j\) satisfies the condition, it is connected from \ (j\) to \ (i ' \) to an edge.
For one path in a path overlay, in addition to the point at the end of the path, that is, the ball at the bottom of a pillar, the other points have a successor, and in the binary we make these points match their successors.
In this case, because the number of paths = number of points at the end of the path = total number of points-number of non-end points = Total number of points-two of the number of matches in the graph, so let's do a binary match.
As the ball number increases, the minimum number of columns required is monotonous, so it can be two points. But two points each time to re-build the maximum flow, if enumerated in sequence, each time only need to add some edge, and then continue to run on the original residual network OK.
As for the output scheme, finally ran the maximum number corresponding to the maximum flow, and then on the right side of the second graph to find the capacity is 1 side (flow from the left to 1 of the traffic, then the right back to the capacity will be + 1), the ball on the top of each ball down, and then output from 1, as long as the current ball From this ball, it starts to output upwards.
Attention:
1. Although the maximum answer is 1600, the total square number may be more than that, for example: \ (1000+1500=2500=500^2\).
1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intmaxn= the+5, opposite= the, maxm=1000000+5, inf=0x3fffffff;5 intN,cnt=1, S,t,max_flow,ans;6 inthead[maxn<<1],q[maxn<<1],lv[maxn<<1],itr[maxn<<1],MATCH[MAXN];7 BOOLissquare[maxn<<1],VIS[MAXN];8 structedge{9 intTo,cap,next;Ten Edge () {} OneEdgeintTo,intCapintnext): To, Cap (CAP), next (next) {} A }G[MAXM]; - voidAdd_edge (intUintVintcap) { -G[++cnt]=edge (V,cap,head[u]); head[u]=CNT; theG[++cnt]=edge (U,0, Head[v]); head[v]=CNT; - } - voidBFs () { - intFront=0, tail=1; +q[front]=s; -memset (lv,-1,sizeofLV); lv[s]=0; + while(front<tail) { A intu=q[front++]; at for(intI=head[u];i;i=G[i].next) { - intv=g[i].to; - if(lv[v]<0&&g[i].cap>0){ -lv[v]=lv[u]+1; -q[tail++]=v; - } in } - } to } + intDfsintUintf) { - if(u==t)returnF; the for(int&i=itr[u];i;i=G[i].next) { * intv=g[i].to; $ if(g[i].cap>0&&lv[u]<Lv[v]) {Panax Notoginseng intD=Dfs (V,min (g[i].cap,f)); - if(d>0){ theg[i].cap-=D; +g[i^1].cap+=D; A returnD; the } + } - } $ return 0; $ } - voidDinic () { - intflow=0, F; the for(BFS ();lv[t]>0; BFS ()) { - for(inti=s;i<=t;i++) itr[i]=Head[i];Wuyi while((F=dfs (s,inf)) >0) flow+=F; the } -max_flow+=flow; Wu } - voidsolve () { Aboutmax_flow=0; $ for(intI=1;; i++){ -ans=i-1; -Add_edge (S,i,1); -Add_edge (I+opposite,t,1); A for(intj=1; j<i;j++)if(Issquare[i+j]) Add_edge (J,i+opposite,1); + dinic (); the if(i-max_flow>n) Break; - } $Memset (Head,0,sizeofHead); Cnt=1; max_flow=0; the for(intI=1; i<=ans;i++){ theAdd_edge (S,i,1); theAdd_edge (I+opposite,t,1); the for(intj=1; j<i;j++)if(Issquare[i+j]) Add_edge (J,i+opposite,1); - } in dinic (); the } the voidprint () { About intp; theprintf"%d\n", ans); the for(intI=opposite+1; i<=ans+opposite;i++) for(intJ=head[i];j;j=g[j].next)if(g[j].cap==1){ thematch[g[j].to]=i-opposite; + Break; - } the for(intI=1; i<=ans;i++)if(!Vis[i]) {Bayip=0; the for(intj=i;j;j=Match[j]) { theprintf"%d", j); -vis[j]=true; - } theprintf"\ n"); the } the } the voidinit () { -scanf"%d",&n); thes=0; t=3200+1; the for(intI=1; i*i<=3200; i++) issquare[i*i]=true; the }94 intMain () { the init (); the solve (); the print ();98 return 0; About}
View Code
Algorithm implementation question 8-4 Magic ball problem (exercise 8-14)
«Problem Description:
Assuming that there are n pillars, it is now necessary to place the ball in the nth column numbered 1,2,3,1⁄4 in the following rules.
(1) The ball can only be placed at the top of a column at a time.
(2) in the same pillar, the sum of the number of any 2 adjacent balls is the total square number.
Try to design an algorithm that calculates the maximum number of balls that can be placed on n poles. For example, a maximum of 4 pillars can be
Put 11 balls.
«Programming Tasks:
For a given n, calculate the maximum number of balls that can be placed on n pillars.
«Data Entry:
The input data is provided by the file Input.txt. The 1th line of the file has 1 positive integer n, which indicates the number of posts.
«Result Output:
At the end of the program, output the maximum number of balls on n poles and the corresponding placement scheme to the file
The output.txt. The first line of the file is the number of balls. The next n rows, each row is the number of a ball on a pillar.
Input File Example
Input.txt
4
Output File Example
Output.txt
11
1 8
2 7 9
3 6 10
4 5 11
Cogs_396_ Magic Ball Problem _ (Minimum path overlay + binary graph matching, network flow 24 question #)