Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <algorithm>
- #include <math.h>
- #include <functional>
- #define x first
- #define y second
- using namespace std;
- typedef pair<double, double> pr;
- double dist(pr a, pr b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }
- int main() {
- ifstream fin("input.txt");
- ofstream fout("output.txt");
- int n; fin >> n;
- vector<pr> vec(n); for (int i = 0; i < n; i++) fin >> vec[i].x >> vec[i].y;
- vector<vector<double>> dp(n, vector<double>(1 << n, 1e9)); dp[0][0] = 0;
- for (int mask = 0; mask < (1 << n); mask++) {
- for (int from = 0; from < n; from++) {
- if (dp[from][mask] == 1e9) continue;
- for (int to = 0; to < n; to++) {
- if (!((mask >> to) & 1))
- if (dp[from][mask] + dist(vec[from], vec[to]) < dp[to][mask | (1 << to)]) {
- dp[to][mask | (1 << to)] = dp[from][mask] + dist(vec[from], vec[to]);
- }
- }
- }
- }
- fout << fixed << dp[0][(1 << n) - 1] << endl;
- function<void(int, int)> print = [&](int cur, int mask) {
- if (cur == 0 && mask == 0) return;
- for (int i = 1; i < n; i++) {
- if (i != cur && dp[cur][mask] == dp[i][(mask & (~(1 << cur)))] + dist(vec[i], vec[cur])) {
- print(i, mask & (~(1 << cur)));
- fout << i + 1 << " "; break;
- }
- }
- };
- print(0, (1 << n) - 1);
- fin.close();
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment