-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP10911.cpp
80 lines (70 loc) · 1.73 KB
/
P10911.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
struct PermutationNode {
PermutationNode *next;
string name;
int x, y;
};
struct PermutationHandler {
PermutationNode *nodes;
PermutationHandler(int size) {
nodes = new PermutationNode[size+1];
for(int i = 0; i < size; ++i) {
cin >> nodes[i+1].name >> nodes[i+1].x >> nodes[i+1].y;
nodes[i].next = &(nodes[i+1]);
}
nodes[size].next = NULL;
}
~PermutationHandler() {
delete[] nodes;
}
PermutationNode* root() {
return &(nodes[0]);
}
};
double dist(PermutationNode const * const a, PermutationNode const * const b) {
int dx = a->x - b->x;
int dy = a->y - b->y;
return sqrt(dx*dx+dy*dy);
}
void run(const int i, const int k, const double cur, double &best, PermutationNode const * const prev, PermutationHandler &ph) {
if(i == k) {
if(cur < best)
best = cur;
return;
}
// try all combinations:
PermutationNode *node = ph.root();
while(node->next != NULL) {
PermutationNode *n = node->next;
// remove n from permutation:
node->next = n->next;
if(i % 2 == 0) {
run(i+1, k, cur, best, n, ph);
}
else {
double add = dist(prev, n);
if(node->name < n->name && cur + add < best) {
run(i+1, k, cur+add, best, NULL, ph);
}
}
// re-insert in permutation and go to next:
node->next = n; // n->next is already set (never changes)
node = n;
}
}
int main() {
int size;
for(int cas = 1; true; ++cas) {
cin >> size;
if(size == 0)
return 0;
PermutationHandler ph(2*size);
double best = 10000000, cur = 0;
run(0, 2*size, cur, best, NULL, ph);
printf("Case %d: %.2lf\n", cas, best);
}
}