求助
查看原帖
求助
871004
Manki23333333楼主2023/8/12 18:25
#include<bits/stdc++.h>

using namespace std;

const int N = 1 << 24, INF = 0x3f3f3f3f;

int sx, sy, n, dp [N], cnt [25] [25], x [25], y [25], oper [N];

int Dis_Sovle (int x1, int y1, int x2, int y2) {
	return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

int main () {
	cin >> sx >> sy >> n;
	
	memset (dp, INF, sizeof dp);
	dp [0] = 0;
	
	for (int i = 1 ; i <= n ; i ++) {
		cin >> x [i] >> y [i];
	}
	
	for (int i = 1 ; i <= n ; i ++) {
		for (int j = 1 ; j <= n ; j ++) {
			cnt [i] [j] = Dis_Sovle (sx, sy, x [i], y [i]) + Dis_Sovle (x [j], y [j], x [i], y [i]) + Dis_Sovle (sx, sy, x [j], y [j]);
		}
	}
	
	for (int sta = 0 ; sta < (1 << n) - 1 ; sta ++) {
		if (dp [sta] == INF) continue;
		
		for (int i = 1 ; i <= n ; i ++) {
			if ((sta >> (i - 1)) == 0) {
				for (int j = 1 ; j <= n ; j ++) {
					if ((sta >> (j - 1)) == 0) {
						if (dp [sta] + cnt [i] [j] < dp [sta | (1 << (i - 1)) | (1 << (j - 1))]){
							oper [sta | (1 << i) | (1 << j)] = sta;
							dp [sta | (1 << (i - 1)) | (1 << (j - 1))] = min (dp [sta | (1 << (i - 1)) | (1 << (j - 1))], dp [sta] + cnt [i] [j]);
                		}
					}
				}
			}
		}
	}
	
	cout << dp [(1 << n) - 1] << "\n";
	
	int now = (1 << n) - 1;
    while (now) {
        cout << "0 " ;
        int next = now ^ oper [now];
        for (int i = 1; i <= n ; i ++ )
            if (next & 1 << (i - 1))
                cout << i << " ";
        now = oper [now];
    }
    cout << "0 \n";
	return 0;
}
2023/8/12 18:25
加载中...