[英]Finding minimum total length of line segments to connect 2N points
I'm trying to solve this problem by bruteforce, but it seems to run very slow when given 7 (which is 2*7 points).我试图通过蛮力来解决这个问题,但是当给定 7(即 2*7 点)时,它似乎运行得很慢。
Note: I only need to run it to maximum 2*8 points注意:我只需要运行到最大 2*8 点
Problem statement:问题陈述:
Given 2*N points in a 2d plane, connect them in pairs to form N line segments.给定二维平面中的 2*N 个点,将它们成对连接以形成 N 个线段。 Minimize the total length of all the line segments.最小化所有线段的总长度。
Example:例子:
Input: 5 10 10 20 10 5 5 1 1 120 3 6 6 50 60 3 24 6 9 0 0输入:5 10 10 20 10 5 5 1 1 120 3 6 6 50 60 3 24 6 9 0 0
Output: 118.4输出:118.4
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
using namespace std;
class point{
public:
double x, y;
};
double getLength(point a, point b){
return hypot((a.x - b.x), (a.y - b.y));
}
static double mini = INT_MAX;
void solve(vector <point> vec, double sum){
double prevSum = sum;
if(sum > mini){
return;
}
if(vec.size() == 2){
sum += getLength(vec[0], vec[1]);
mini = min(mini, sum);
return;
}
for(int i = 0; i < vec.size() - 1; i++){
for(int j = i + 1; j < vec.size(); j++){
sum = prevSum;
vector <point> temp = vec;
sum += getLength(temp[i], temp[j]);
temp.erase(temp.begin() + j);
temp.erase(temp.begin() + i);
solve(temp, sum);
}
}
}
int main(){
point temp;
int input;
double sum = 0;
cin >> input;
vector<point> vec;
for(int i = 0; i < 2 * input; i++){
cin >> temp.x >> temp.y;
vec.push_back(temp);
}
solve(vec, sum);
cout << fixed << setprecision(2) << mini << endl;
}
How can I speed up this code ?我怎样才能加速这个代码?
I don't think this is what you are looking for but I mention it for completeness sake anyway.我不认为这就是你要找的东西,但无论如何我还是为了完整起见提到它。 The problem can be formulated as a Mixed Integer Programming (MIP) problem.该问题可以表述为混合整数规划(MIP) 问题。
We have distances:我们有距离:
d(i,j) = distance between point i and j (only needed for i<j)
and decision variables和决策变量
x(i,j) = 1 if points i and j are connected (only needed for i<j)
0 otherwise
Then we can write:然后我们可以写:
Solving this problem can be done with widely available MIP solvers and leads to proven optimal solutions.可以使用广泛可用的 MIP 求解器来解决此问题,并得出经过验证的最佳解决方案。 A small example with 50 points:一个50分的小例子:
You can solve this iteratively by using next_permutation() to go through all the permutations one by one.您可以通过使用 next_permutation() 逐一遍历所有排列来迭代地解决此问题。 Apologies for the messy code, but this should show you how to do it:为凌乱的代码道歉,但这应该告诉你如何去做:
struct Point {
Point(int x, int y) : x(x), y(y) {
}
bool operator< (const Point& rhs) {
const int key1 = y * 1000 + x;
const int key2 = rhs.y * 1000 + rhs.x;
return key1 < key2;
}
double dist(const Point& next) {
const double h = (double)(next.x - x);
const double v = (double)(next.y - y);
return sqrt(h*h + v*v);
}
int x, y;
};
You need the operator so you have some sort of sorting key for your points, so next_permutation can go through them in lexicographical increasing order.您需要运算符,以便为您的点提供某种排序键,因此 next_permutation 可以按字典序递增顺序遍历它们。 double getShortestDist(std::vector p) {双 getShortestDist(std::vector p) {
double min = 200000;
std::sort(p.begin(), p.end());
while(std::next_permutation(p.begin(), p.end())) {
double sum = 0.0;
for (int i = 0; i < p.size(); i+= 2) {
sum += p[i].dist(p[i+1]);
}
if (sum < min) {
min = sum;
}
}
return min;
}
int main(int argc, char*argv[]) {
static const int arr[] = {
10, 10, 20, 10, 5, 5, 1, 1, 120, 3, 6, 6, 50, 60, 3, 24, 6, 9, 0, 0
};
std::vector<Point> test;
for (int i = 0; i < 20; i += 2) {
test.push_back(Point(arr[i], arr[i+1]));
printf("%d %d\n", arr[i], arr[i+1]);
}
printf("Output: %d, %f", test.size(), getShortestDist(test));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.