1739: Magic Ball Problem - Minimum Path Coverage

Topic:

Assuming there are n pillars, you will now place balls numbered 1, 2, 3,... In the N pillars in turn according to the following rules.(1) Play the ball only on the top of a post at a time.(2) In the same post, the sum of the numbers of any two adjacent balls is a perfect square number.Try to design an algorithm to calculate the maximum number of balls that can be placed on N pillars.For example, you can put up to 11 balls on four columns.Programming task: For a given n, calculate the maximum number of balls that can be placed on N pillars.

Ideas:

The initial number of points is 1, each time the number of points is added one, a new edge is added, and the minimum path coverage is calculated once to determine if the value is greater than n, then exit, not continue the cycle.

```#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

const int maxn = 10010;
const int INF = 0x3f3f3f3f;

struct Edge {
int from, to, cap, flow, flag;
};
struct Dinic {
int s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init() {
edges.clear();
for (int i = 0; i < maxn; i++) G[i].clear();
}
void addedge(int from, int to, int cap, int flag) {
edges.push_back(Edge{from, to, cap, 0, flag});
edges.push_back(Edge{to, from, 0, 0, flag});
int m = edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool bfs() {
memset(vis, 0, sizeof(vis));
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while (!q.empty()) {
int x = q.front(); q.pop();
for (int i = 0; i < G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || a == 0) return a;
int flow = 0, f;
for (int &i = cur[x]; i < G[x].size(); i++) {
Edge &e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0) {
e.flow += f;
edges[G[x][i]^1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int maxflow(int s, int t) {
this->s = s, this->t = t;
int flow = 0;
while (bfs()) {
memset(cur, 0, sizeof(cur));
flow += dfs(s, INF);
}
return flow;
}
}ac;

vector<int> G[maxn];
int vis[maxn];

void dfs(int u) {
if (vis[u]) return;
vis[u] = true;
printf("%d ", u);
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
dfs(v);
}
}

int main() {
int n;
scanf("%d", &n);
int s = 0, t = 10001;
int ans = 1, maxf = 0;
while (true) {
for (int i = 1; i < ans; i++) {
int x = sqrt(i+ans);
if (x * x != i + ans) continue;
}
maxf += ac.maxflow(s, t);
if (ans - maxf > n) break;
ans++;
}
printf("%d\n", ans-1);
memset(vis, 0, sizeof(vis));
for (int i = 0; i < ac.edges.size(); i += 2) {
if (ac.edges[i].flag == 1 && ac.edges[i].flow == 1) {
int u = ac.edges[i].from, v = ac.edges[i].to;
G[u].push_back(v-5000);
}
}
for (int i = 1; i < ans; i++) {
if (vis[i]) continue;
dfs(i);
printf("\n");
}
return 0;
}
```

Tags: Programming

Posted on Wed, 05 Feb 2020 08:11:53 -0800 by kujtim