突然对 K 分图判定有了新的想法。
K 分图判定,或者图着色问题是 NPC。如果你不知道这是什么,你需要百度百科。
本人只解决三分图,并给出代码和向更高的 K 的拓展办法。
直接说思路:制作物理引擎,建立在三维空间上,每一个点为质点。
质点受到与运动方向相反的作用力,让系统总能量减少。(人话:不需要让点抖来抖去。)
若两点间连边,其对应质点当前距离大于 ,则有引力,否则,有斥力,力的大小越近或越远就越大。(人话:边是弹簧。)
每个点受到 三个点的引力,随距离增加而减少。(人话:需要三个引力势阱。)
每个点每帧会出现随机速度偏转,为了对当前构造的方案进行反悔。(人话:避免需要去 的点卡在 。)
K 分图判定需要 维空间中选正 维面体的顶点作为势阱。例如 需要在 维空间中选三维三菱锥的顶点作为势阱。
思路说完了,然后你会发现它假了,如果有早早合并到位的东西把一个东西卡在外面,但是需要让很多一些东西反悔,就做不到了。在 是正确率高达 ,耗时约 毫秒。
代码
#include <bits/stdc++.h>
using namespace std;
#define float long double
float damping_constant = 0.875;
float default_delta_time = 0.016;
float elastic_coefficient = 0.5;
float max_move_dist = 0.1;
float G = 0.3;
float random_deflection = 0.1;
int tot_round = 100000, step;
// #define debug 100
// #define debug2 10000
// #define print_construction
struct vector3_t {
float x, y, z;
vector3_t(float _x = 0, float _y = 0, float _z = 0) : x(_x), y(_y), z(_z) {}
vector3_t operator-() const {
return {-x, -y, -z};
}
vector3_t operator+(const vector3_t &other) const {
return {x + other.x, y + other.y, z + other.z};
}
vector3_t operator-(const vector3_t &other) const {
return {x - other.x, y - other.y, z - other.z};
}
vector3_t operator*(float scalar) const {
return {x * scalar, y * scalar, z * scalar};
}
friend vector3_t operator*(float scalar, const vector3_t &vec) {
return {vec.x * scalar, vec.y * scalar, vec.z * scalar};
}
vector3_t &operator+=(const vector3_t &other) {
x += other.x;
y += other.y;
z += other.z;
return *this;
}
vector3_t &operator-=(const vector3_t &other) {
x -= other.x;
y -= other.y;
z -= other.z;
return *this;
}
vector3_t &operator*=(float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
float magnitude() const {
return sqrt(x * x + y * y + z * z);
}
friend ostream &operator<<(ostream &os, const vector3_t &vec) {
os << "(" << vec.x << ", " << vec.y << ", " << vec.z << ")";
return os;
}
} c[3] = {{0, 0, 0}, {2, 0, 0}, {1, sqrt(3), 0}};
struct particle3_t {
vector3_t position, velocity;
particle3_t(float _x = 0, float _y = 0, float _z = 0, float _vx = 0, float _vy = 0, float _vz = 0)
: position(_x, _y, _z),
velocity(_vx, _vy, _vz) {}
particle3_t &move(float t) {
position += t * velocity;
velocity *= damping_constant;
return *this;
}
friend ostream &operator<<(ostream &os, const particle3_t &p) {
os << "[Pos: " << p.position << " Vel: " << p.velocity << "]";
return os;
}
};
void gravitate(particle3_t &a, float delta_time) {
for (int i = 0; i < 3; i++) {
vector3_t dir = c[i] - a.position;
float dist = dir.magnitude();
if (dist == 0) continue;
dir *= (1.0 / dist);
float force = G / ((dist + 0.1) * (dist + 0.1));
a.velocity += dir * force * delta_time;
}
}
void elasticate(particle3_t &a, particle3_t &b, float delta_time) {
vector3_t dir = b.position - a.position;
float dist = dir.magnitude();
#ifdef debug
if (step % debug == 0) {
cout << "segment(" << a.position << ", " << b.position << "),";
}
#endif
if (dist < 2) {
float overlap = min<double>(10000, 4 / (dist + 1e-9) - 2);
a.velocity -= (overlap * elastic_coefficient) * dir * delta_time;
b.velocity += (overlap * elastic_coefficient) * dir * delta_time;
} else {
float overlap = min<double>(10000, dist - 2);
a.velocity += (overlap * elastic_coefficient) * dir * delta_time;
b.velocity -= (overlap * elastic_coefficient) * dir * delta_time;
}
}
float rand_float(float l, float r) {
return l + (r - l) * ((float)(rand()) / RAND_MAX);
}
int n, m;
vector<int> ans;
vector<pair<int, int> > edge;
vector<particle3_t> particles;
float delta_time = default_delta_time;
float R, sqrt3 = sqrt(3);
int main() {
srand(chrono::steady_clock::now().time_since_epoch().count());
/*
// Test gravitational potential hydrazine
particle3_t a{rand_float(-1, 3), rand_float(-1, 3), rand_float(-1, 3)};
int count = 0;
while (true) {
gravitate(a, default_delta_time);
a.move(default_delta_time);
// cout << a << endl;
count++;
if (a.velocity.magnitude() < 0.001) break;
}
cout << a << " " << count << endl;
cout << min({(c[0] - a.position).magnitude(),
(c[1] - a.position).magnitude(),
(c[2] - a.position).magnitude()}) << endl;
*/
cin >> n >> m;
R = pow(n, 1.0 / 3);
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
edge.push_back({u - 1, v - 1});
}
for (int i = 1; i <= n; i++) {
particles.push_back(particle3_t{rand_float(1 - R, 1 + R), rand_float(sqrt3 - R, sqrt3 + R), rand_float(-R, R)});
}
float prev_max_speed = 1e9;
for (step = 1; step <= tot_round; step++) {
#ifdef debug
if (step % debug == 0) {
cout << "step = " << step << endl;
for (int i = 0; i < n; i++) {
cout << i << " : " << particles[i] << '\n';
}
}
#endif
delta_time = default_delta_time;
for (int i = 0; i < n; i++) {
float speed = particles[i].velocity.magnitude();
float delta_time_limit = max_move_dist / (speed + 1e-9);
if (delta_time > delta_time_limit) {
delta_time = delta_time_limit;
}
}
#ifdef debug
if (step % debug == 0) {
cout << "delta_time = " << delta_time << endl;
}
#endif
for (int i = 0; i < n; i++) {
gravitate(particles[i], delta_time);
}
#ifdef debug
if (step % debug == 0) {
cout << "after gravitate:" << endl;
for (int i = 0; i < n; i++) {
cout << i << " : " << particles[i] << '\n';
}
cout << "{";
}
#endif
for (pair<int, int> i : edge) {
elasticate(particles[i.first], particles[i.second], delta_time);
}
#ifdef debug
if (step % debug == 0) {
cout << c[0] << ',' << c[1] << ',' << c[2] << "}\n";
cout << "after elasticate:" << endl;
for (int i = 0; i < n; i++) {
cout << i << " : " << particles[i] << '\n';
}
}
#endif
#ifdef debug2
if (step % debug2 == 0) {
cout << "{";
for (pair<int, int> i : edge) {
cout << "segment(" << particles[i.first].position << ", " << particles[i.second].position << "), ";
}
cout << c[0] << ", " << c[1] << ", " << c[2] << "}\n";
}
#endif
float max_speed = 0;
for (int i = 0; i < n; i++) {
particles[i].velocity.z += rand_float(-random_deflection, random_deflection);
particles[i].velocity.x += rand_float(-random_deflection, random_deflection);
particles[i].velocity.y += rand_float(-random_deflection, random_deflection);
particles[i].move(delta_time);
float speed = particles[i].velocity.magnitude();
if (speed > max_speed) {
max_speed = speed;
}
}
if (max_speed < 0.01 && prev_max_speed < 0.01) {
break;
}
prev_max_speed = max_speed;
}
#ifdef debug2
for (int i = 0; i < n; i++) {
cout << i + 1 << " : " << particles[i].position << ' ';
float dis0 = (c[0] - particles[i].position).magnitude();
float dis1 = (c[1] - particles[i].position).magnitude();
float dis2 = (c[2] - particles[i].position).magnitude();
cout << min({dis0, dis1, dis2}) << endl;
}
cout << endl;
#endif
ans.resize(n);
for (int i = 0; i < n; i++) {
float dis0 = (c[0] - particles[i].position).magnitude();
float dis1 = (c[1] - particles[i].position).magnitude();
float dis2 = (c[2] - particles[i].position).magnitude();
if (dis0 < dis1 && dis0 < dis2) {
ans[i] = 1;
#ifdef print_construction
cout << 1 << ' ';
#endif
} else if (dis1 < dis0 && dis1 < dis2) {
ans[i] = 2;
#ifdef print_construction
cout << 2 << ' ';
#endif
} else {
ans[i] = 3;
#ifdef print_construction
cout << 3 << ' ';
#endif
}
}
for (pair<int, int> i : edge) {
if (ans[i.first] == ans[i.second]) {
#ifdef print_construction
cerr << "Error: same color edge exist!" << endl;
cerr << i.first + 1 << " " << i.second + 1 << endl;
cerr << particles[i.first] << " " << particles[i.second] << endl;
#else
cout << "Wrong" << endl;
return 1;
#endif
}
}
#ifndef print_construction
cout << "Correct" << endl;
#endif
return 0;
}数据生成器 和 拍子
#include <bits/stdc++.h>
using namespace std;
int main()
{
srand(chrono::steady_clock::now().time_since_epoch().count());
int n = 3, m = 10;
// cin >> n >> m;
cout << n * 3 << ' ' << m << '\n';
for (int i = 1; i <= m; i++)
{
int u = rand() % n + 1;
int v = rand() % n + 1;
int t = rand() % 3;
if (t == 0) cout << u << ' ' << v + n << '\n';
if (t == 1) cout << u + n << ' ' << v + 2 * n << '\n';
if (t == 2) cout << u + n << ' ' << v + 2 * n << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
int main() {
system("g++ -O2 -std=c++14 -static -o data.exe data.cpp");
system("g++ -O2 -std=c++14 -static -o main.exe main.cpp");
for (int i = 1, count = 0; i <= 100000; i++)
{
system("data.exe > input.txt");
int ret = system("main.exe < input.txt");
count += ret;
cout << (float)count / i * 100 << "%" << endl;
}
return 0;
}发现似乎卡在外面的情况只需要让点可以轻松绕着一个环转即可实现非常好的反悔,把引力势阱之间连通?
于是改计算引力的部分,三个引力中心的引力大幅削弱,并加入一个环形引力场。不会写环形引力场,那就让每个点连向中间一个弹簧,并增加向 平面的加速度。
然后我加了一个每次循环判断一下,如果合法就跳出。可以让 跑得又快又对。甚至 都能跑得飞快。能解决随机 。
但是效率还是太低了
#include <bits/stdc++.h>
using namespace std;
#define float long double
constexpr float damping_constant = 0.9;
constexpr float default_delta_time = 0.016;
constexpr float elastic_coefficient = 1.0;
constexpr float max_move_dist = 0.05;
constexpr float Gc = 0.003, Gr = 0.1;
constexpr float random_deflection = 0.1;
constexpr float sqrt3 = sqrt(3);
constexpr unsigned long long tot_round = 1000000;
unsigned long long step;
// #define debug 100
#define debug2 10000
#define print_construction
struct vector3_t {
float x, y, z;
vector3_t(float _x = 0, float _y = 0, float _z = 0) : x(_x), y(_y), z(_z) {}
vector3_t operator-() const {
return {-x, -y, -z};
}
vector3_t operator+(const vector3_t &other) const {
return {x + other.x, y + other.y, z + other.z};
}
vector3_t operator-(const vector3_t &other) const {
return {x - other.x, y - other.y, z - other.z};
}
vector3_t operator*(float scalar) const {
return {x * scalar, y * scalar, z * scalar};
}
friend vector3_t operator*(float scalar, const vector3_t &vec) {
return {vec.x * scalar, vec.y * scalar, vec.z * scalar};
}
vector3_t &operator+=(const vector3_t &other) {
x += other.x;
y += other.y;
z += other.z;
return *this;
}
vector3_t &operator-=(const vector3_t &other) {
x -= other.x;
y -= other.y;
z -= other.z;
return *this;
}
vector3_t &operator*=(float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
float magnitude() const {
return sqrt(x * x + y * y + z * z);
}
friend ostream &operator<<(ostream &os, const vector3_t &vec) {
os << "(" << vec.x << ", " << vec.y << ", " << vec.z << ")";
return os;
}
} c[3] = {{0, 0, 0}, {2, 0, 0}, {1, sqrt(3), 0}};
struct particle3_t {
vector3_t position, velocity;
particle3_t(float _x = 0, float _y = 0, float _z = 0, float _vx = 0, float _vy = 0, float _vz = 0)
: position(_x, _y, _z),
velocity(_vx, _vy, _vz) {}
particle3_t &move(float t) {
position += t * velocity;
velocity *= damping_constant;
return *this;
}
friend ostream &operator<<(ostream &os, const particle3_t &p) {
os << "[Pos: " << p.position << " Vel: " << p.velocity << "]";
return os;
}
};
void elasticate(particle3_t &a, particle3_t &b, float length, float delta_time) {
vector3_t dir = b.position - a.position;
float dist = dir.magnitude();
#ifdef debug
if (step % debug == 0) {
cout << "segment(" << a.position << ", " << b.position << "),";
}
#endif
if (dist < length) {
float overlap = min<double>(10000, length * length / (dist + 1e-9) - length);
a.velocity -= (overlap * elastic_coefficient) * dir * delta_time;
b.velocity += (overlap * elastic_coefficient) * dir * delta_time;
} else {
float overlap = min<double>(10000, dist - length);
a.velocity += (overlap * elastic_coefficient) * dir * delta_time;
b.velocity -= (overlap * elastic_coefficient) * dir * delta_time;
}
}
particle3_t center{1, sqrt(3) / 3, 0};
float dis_center_c = (center.position - c[0]).magnitude();
void gravitate(particle3_t &a, float delta_time) {
for (int i = 0; i < 3; i++) {
vector3_t dir = c[i] - a.position;
float dist = dir.magnitude();
dir *= (1.0 / dist);
float force = Gc / ((dist + 0.1) * (dist + 0.1));
a.velocity += dir * force * delta_time;
}
elasticate(a, center, dis_center_c, delta_time);
a.velocity.z -= a.position.z * Gr * delta_time;
}
float rand_float(float l, float r) {
return l + (r - l) * ((float)(rand()) / RAND_MAX);
}
int n, m;
vector<int> ans;
vector<pair<int, int> > edge;
vector<particle3_t> particles;
float delta_time = default_delta_time;
int main() {
srand(chrono::steady_clock::now().time_since_epoch().count());
/*
// Test gravitational potential hydrazine
particle3_t a{rand_float(-1, 3), rand_float(-1, 3), rand_float(-1, 3)};
int count = 0;
while (true) {
gravitate(a, default_delta_time);
a.move(default_delta_time);
// cout << a << endl;
count++;
if (a.velocity.magnitude() < 0.001) break;
}
cout << a << " " << count << endl;
cout << min({(c[0] - a.position).magnitude(),
(c[1] - a.position).magnitude(),
(c[2] - a.position).magnitude()}) << endl;
*/
cin >> n >> m;
float R = pow(n, 1.0 / 3);
for (int i = 1, u, v; i <= m; i++) {
cin >> u >> v;
edge.push_back({u - 1, v - 1});
}
for (int i = 1; i <= n; i++) {
particles.push_back(particle3_t{rand_float(1 - R, 1 + R), rand_float(sqrt3 - R, sqrt3 + R), rand_float(-R, R)});
}
// float prev_max_speed = 1e9;
ans.resize(n);
for (step = 1; step <= tot_round; step++) {
#ifdef debug
if (step % debug == 0) {
cout << "step = " << step << endl;
for (int i = 0; i < n; i++) {
cout << i << " : " << particles[i] << '\n';
}
}
#endif
delta_time = default_delta_time;
for (int i = 0; i < n; i++) {
float speed = particles[i].velocity.magnitude();
float delta_time_limit = max_move_dist / (speed + 1e-9);
if (delta_time > delta_time_limit) {
delta_time = delta_time_limit;
}
}
#ifdef debug
if (step % debug == 0) {
cout << "delta_time = " << delta_time << endl;
}
#endif
for (int i = 0; i < n; i++) {
gravitate(particles[i], delta_time);
}
#ifdef debug
if (step % debug == 0) {
cout << "after gravitate:" << endl;
for (int i = 0; i < n; i++) {
cout << i << " : " << particles[i] << '\n';
}
cout << "{";
}
#endif
for (pair<int, int> i : edge) {
elasticate(particles[i.first], particles[i.second], 2, delta_time);
}
#ifdef debug
if (step % debug == 0) {
cout << c[0] << ',' << c[1] << ',' << c[2] << "}\n";
cout << "after elasticate:" << endl;
for (int i = 0; i < n; i++) {
cout << i << " : " << particles[i] << '\n';
}
}
#endif
#ifdef debug2
if (step % debug2 == 0) {
cout << "{";
for (pair<int, int> i : edge) {
cout << "segment(" << particles[i.first].position << ", " << particles[i.second].position << "), ";
}
cout << c[0] << ", " << c[1] << ", " << c[2] << "}\n";
}
#endif
// float max_speed = 0;
for (int i = 0; i < n; i++) {
particles[i].velocity.z += rand_float(-random_deflection, random_deflection);
particles[i].velocity.x += rand_float(-random_deflection, random_deflection);
particles[i].velocity.y += rand_float(-random_deflection, random_deflection);
particles[i].move(delta_time);
// float speed = particles[i].velocity.magnitude();
// if (speed > max_speed) {
// max_speed = speed;
// }
}
for (int i = 0; i < n; i++) {
float dis0 = (c[0] - particles[i].position).magnitude();
float dis1 = (c[1] - particles[i].position).magnitude();
float dis2 = (c[2] - particles[i].position).magnitude();
if (dis0 < dis1 && dis0 < dis2) {
ans[i] = 1;
} else if (dis1 < dis0 && dis1 < dis2) {
ans[i] = 2;
} else {
ans[i] = 3;
}
}
bool flag = true;
for (pair<int, int> i : edge) {
if (ans[i.first] == ans[i.second]) {
flag = false;
break;
}
}
if (flag) {
break;
}
// if (max_speed < 0.01 && prev_max_speed < 0.01) {
// break;
// }
// prev_max_speed = max_speed;
}
#ifdef debug2
for (int i = 0; i < n; i++) {
cout << i + 1 << " : " << particles[i].position << ' ';
float dis0 = (c[0] - particles[i].position).magnitude();
float dis1 = (c[1] - particles[i].position).magnitude();
float dis2 = (c[2] - particles[i].position).magnitude();
cout << min({dis0, dis1, dis2}) << endl;
}
cout << endl;
#endif
for (int i = 0; i < n; i++) {
float dis0 = (c[0] - particles[i].position).magnitude();
float dis1 = (c[1] - particles[i].position).magnitude();
float dis2 = (c[2] - particles[i].position).magnitude();
if (dis0 < dis1 && dis0 < dis2) {
ans[i] = 1;
#ifdef print_construction
cout << 1 << ' ';
#endif
} else if (dis1 < dis0 && dis1 < dis2) {
ans[i] = 2;
#ifdef print_construction
cout << 2 << ' ';
#endif
} else {
ans[i] = 3;
#ifdef print_construction
cout << 3 << ' ';
#endif
}
}
for (pair<int, int> i : edge) {
if (ans[i.first] == ans[i.second]) {
#ifdef print_construction
cerr << "Error: same color edge exist!" << endl;
cerr << i.first + 1 << " " << i.second + 1 << endl;
cerr << particles[i.first] << " " << particles[i.second] << endl;
#else
cout << "Wrong" << endl;
return 1;
#endif
}
}
#ifndef print_construction
cout << "Correct" << endl;
#endif
return 0;
}物理引擎打不过 暴搜。
可能是我调参比较弱,可以调整的参数包括:阻尼、单帧最大时间、弹力系数、单帧最大单质点移动距离、三个引力场的引力系数, 平面的引力系,单帧速度扰动。都在代码开始处。注意根号三不是可以调整的参数,它就是根号三。