-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path216-Getting in Line.cpp
67 lines (64 loc) · 1.92 KB
/
216-Getting in Line.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
#include <bits/stdc++.h>
using namespace std;
int n,x,y;
vector<pair<int,int>> computers;
double memo[1<<8][8];
double decision[1<<8][8];
double euclidean(int i, int j){
x = computers[i].first-computers[j].first;
y = computers[i].second-computers[j].second;
return sqrt(x*x + y*y);
}
double tsp(int pos, int bitmask){
if(bitmask == ((1<<n)-1)) return 0;
if(memo[bitmask][pos] >= 0) return memo[bitmask][pos];
double res = 1e8;
for(int i=0;i<n;i++)
if(!(bitmask & (1<<i))){
double cost = tsp(i,bitmask | (1<<i)) + euclidean(pos,i);
if(cost < res){
res = cost;
decision[bitmask][pos] = i;
}
}
return memo[bitmask][pos] = res;
}
int main()
{
int tc=1;
while(scanf("%d",&n),n){
computers.clear();
memset(memo,-1,sizeof memo);
memset(decision,-1,sizeof decision);
for(int i=0;i<n;i++){
cin >> x >> y;
computers.push_back({x,y});
}
printf("**********************************************************\n");
printf("Network #%d\n",tc++);
double best = 1e8;
int bestStart = 0;
for(int i=0;i<n;i++){
double cost = tsp(i,1<<i);
if(cost < best){
best = cost;
bestStart = i;
}
}
int cur = bestStart;
int curState = 1<<bestStart;
double totalFeet = 0;
while(curState != ((1<<n)-1)){
int x1,x2,y1,y2;
int nxt = decision[curState][cur];
tie(x1,y1) = computers[cur];
tie(x2,y2) = computers[nxt];
double dist = euclidean(cur,nxt)+16.0;
totalFeet += dist;
printf("Cable requirement to connect (%d,%d) to (%d,%d) is %.2f feet.\n",x1,y1,x2,y2,dist);
curState ^= 1<<nxt;
cur = nxt;
}
printf("Number of feet of cable required is %.2f.\n",totalFeet);
}
}