Magic Ball problems (network flow 24 questions)

Problem Description:

Suppose there are n columns, the following rules are now Yaoan the n columns sequentially into numbered 1,2,3, ... ball.

(1) You can only put the ball at the top of a pillar.

(2) in a same column, any two adjacent balls is the sum of the number of perfect squares.

Try to design an algorithm to calculate the maximum number of balls can be put on the n columns. For example, in the four columns can hold up to 11 balls.

<< programming tasks:

For a given n, calculating the maximum number of balls can be put on the n columns.

4<=n<=55

answer

Considering the increased number of columns increases when a single ball is not critical, and can be added to a ball a determination requires several column installed under.

Each ball split into the two points, two balls into each other around the square of the number of connected edges, in order to ensure even only once from the large to the small side is connected.

The question then becomes minimal path coverage, the number of paths is the number of columns.

Ball enumeration of m, the number of paths when the answer is greater than the number m-1 column, as to the program on the output from a path of traversal like.

In order not to timeout, we add a new ball, ran the ball once from augmenting path can be, which is why from large to small even edge.

#include<bits/stdc++.h>
using namespace std;

const int maxn=3605;
int n,m;
int vis[maxn],match[maxn],timer;
bool square[maxn<<1];
vector<int> e[maxn];

void init(){
  timer=0;
  memset(match,0,sizeof(match));
  memset(vis,0,sizeof(vis));
}

bool dfs(int u){
  if(vis[u]==timer) return false;
  vis[u]=timer;
  for(unsigned int i=0;i<e[u].size();i++){
    int v=e[u][i];
    if(!match[v]||dfs(match[v])){
      match[v]=u;
      return true;
    }
  }
  return false;
}

void get(int u){
  printf("%d ",u);
  vis[u]=timer;
  if(!match[u+1800]){putchar(10);return ;}
  get(match[u+1800]);
}

void print(){
  ++timer;
  for(int i=1;i<m;i++)
    if(vis[i]!=timer) get(i);
}

int main(){
  for(int i=1;i*i<=7200;i++)
   square[i*i]=true;
  scanf("%d",&n);
  int ans=0;
  while(++m){
    for(int i=1;i<m;i++)
     if(square[i+m]) e[m].push_back(i+1800);
    ++timer;
    if(dfs(m)) ans++;
    if(m-ans>n) {printf("%d\n",m-1);print();exit(0);}
  }
}
View Code

 There greedy method, there can put pillars put, or to the newly opened (correctness do not know).

There are number of balls seemingly law 2 + 2 + 4 + 4 + 6 + 6 + ....

Guess you like

Origin www.cnblogs.com/sto324/p/11334014.html