标签:通过 mat continue pre get space == auto tin
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 33;
int mp[N][N];
int sx, sy;
struct node {
int x, y;
}a[N];
int dp[1 << 25];
int pre[1 << 25];
int dis(int i, int j) {
return (a[i].x - a[j].x ) * (a[i].x - a[j].x ) + (a[i].y - a[j].y ) * (a[i].y - a[j].y );
}
vector<int>ans;
void solve() {
cin >> sx >> sy;
int n;cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i].x >> a[i].y;
a[0] = {sx, sy};
int cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i==j)continue;
mp[i][j] = (a[i].x- a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y);
}
}
int t = 0;
memset(dp, 0x3f, sizeof dp);
memset(pre, -1, sizeof pre);
dp[0] = 0;
int inf = 0x3f3f3f3f3f3f3f3f;
for (int s = 0; s < 1 << n; s++) {
if (dp[s] == inf)continue;
for (int i = 1; i <= n; i++) {
if ( s & 1 << (i-1))continue;
for (int j = 1; j <= n; j++) {
if ( s & 1 << (j - 1)) continue;
if (dp[s | (1 << (i-1)) | (1 << (j-1))] > dp[s] + dis(j, 0) + dis(i, j) + dis(i, 0)) {
dp[s | (1 << (i-1)) | (1 << (j-1))] = dp[s] + dis(j, 0) + dis(i, j) + dis(i, 0);
pre[s | (1 << (i-1)) | (1 << (j-1))] = s;
}
}
break;
}
}
int now = (1 << n) - 1;
ans.push_back(0);
while (now != -1) {
int s = pre[now];
if (s == -1)s = 0;
bool f = 1;
for (int i = 1; i <= n; i++) {
if ( ((s >> (i-1) ) & 1) != ((now >> (i-1) ) & 1)) {
ans.push_back(i);
f = 0;
}
}
ans.push_back(0);
now = s;
if (s == 0)break;
}
cout << dp[ (1 << n) -1] << endl;
for (auto iter: ans) cout << iter << " ";
}
signed main() {
int t = 1;
while (t--) solve();
return 0;
}
标签:通过 mat continue pre get space == auto tin
原文地址:https://www.cnblogs.com/Xiao-yan/p/14539934.html