-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy path10911.cpp
54 lines (46 loc) · 1.15 KB
/
10911.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
dynamic programming
difficulty: easy
date: 24/Oct/2020
hint: use bitmasks
by: @brpapa
*/
#include <bits/stdc++.h>
#define bit_is_on(S, i) (S & (1 << i))
using namespace std;
typedef long long ll;
const double INF = 1 << 30;
int N;
vector<pair<int,int>> coords;
double dist(pair<int,int> s, pair<int,int> t) {
double dx = s.first - t.first;
double dy = s.second - t.second;
return sqrt(dx*dx + dy*dy);
}
vector<double> memo;
double dp(int mask) {
// conjunto de estudantes que já estão em um time
if (mask == (1 << N)-1) return 0;
double &ans = memo[mask];
if (ans != -1) return ans;
ans = INF;
int i = 0; while (bit_is_on(mask, i)) i++;
for (int j = i+1; j < N; j++)
if (!bit_is_on(mask, j))
ans = min(ans, dist(coords[i], coords[j]) + dp(mask | (1 << i) | (1 << j)));
return ans;
}
int main() {
int t = 1;
while (cin >> N && N) {
N <<= 1;
coords.resize(N);
for (int n = 0; n < N; n++) {
string _; cin >> _;
cin >> coords[n].first >> coords[n].second;
}
memo.assign(1 << N, -1);
printf("Case %d: %.2f\n", t++, dp(0));
}
return 0;
}