-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgDP.cpp
More file actions
203 lines (174 loc) · 3.44 KB
/
Copy pathAlgDP.cpp
File metadata and controls
203 lines (174 loc) · 3.44 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*************************************************************************
> File Name: bitonic_tour_dp_alg.cpp
> Author: Wang Zhifu
> Mail: 151220117@smail.nju.edu.cn
> Creation Time: 2017-07-09 13:36
> Description:
************************************************************************/
#include <iostream>
#include <assert.h>
#include <cmath>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
//#define DEBUG
struct point
{
double x;
double y;
};
vector<point>v;
double dis(point a, point b) {
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
int main()
{
int num;//the number of points
cin >> num;
for (int i = 0; i < num; i++)
{
point temp;
double temp_x, temp_y;
cin >> temp_x;
cin >> temp_y;
temp.x = temp_x;
temp.y = temp_y;
v.push_back(temp);
}
double **d = new double*[num + 1];
int **no_list = new int*[num + 1];
for (int i = 1; i <= num; i++) {
d[i] = new double[num + 1];
}
for (int i = 1; i <= num; i++) {
no_list[i] = new int[num + 1];
}
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num; j++) {
no_list[i][j] = -1;
}
}
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num; j++) {
d[i][j] = 0;
}
}
d[1][2] = dis(v[0], v[1]);
for (int i = 1; i <= num; i++) {
for (int j = i + 1; j <= num; j++) {
if (i < j - 1) {
d[i][j] = d[i][j - 1] + dis(v[j - 2], v[j - 1]);
}
else if (i == j - 1) {
double min_dis = d[1][j - 1] + dis(v[0], v[j - 1]);
int no = 1;
for (int k = 2; k < j - 1; k++) {
double tmp_dis = dis(v[k - 1], v[j - 1]);
if (d[k][j - 1] + tmp_dis < min_dis) {
min_dis = d[k][j - 1] + dis(v[k - 1], v[j - 1]);
no = k;
}
}
d[i][j] = min_dis;
no_list[i][j] = no;
#ifdef DEBUG
cout << "j->k:" << j << "->" << no << endl;
cout << "i:" << i << " " << "j:" << j << " k: " << no << endl;
#endif // DEBUG
}
#ifdef DEBUG
else {
assert(0);
}
#endif // DEBUG
}
}
#ifdef DEBUG
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num; j++) {
cout << d[i][j] << " ";
}
cout << endl;
}
#endif // DEBUG
#ifdef DEBUG
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= num; j++) {
cout << no_list[i][j] << " ";
}
cout << endl;
}
#endif // DEBUG
double cost = 0;
cost = d[num - 1][num] + dis(v[num - 2], v[num - 1]);
set<int> s1;
set<int> s2;
for (int i = 0; i < num; i++) {
s1.insert(i);
}
int i = num - 1, j = num;
s1.erase(num - 1);
s2.insert(num - 1);
while (no_list[i][j] != -1) {
s1.erase(no_list[i][j] - 1);
s2.insert(no_list[i][j] - 1);
i = no_list[i][j] < i ? no_list[i][j] : i;
j = no_list[i][j] >= i ? no_list[i][j] : i;
}
set<int>::iterator it = s1.find(1);
set<int>::reverse_iterator itr;
s1.insert(0);
s2.insert(0);
if (it == s1.end()) {
#ifdef DEBUG
cout << "1 in set2" << endl;
#endif // DEBUG
for (it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
//cout << endl;
for (itr = s1.rbegin(); itr != s1.rend(); itr++)
{
cout << *itr << " ";
}
cout << endl;
}
else {
#ifdef DEBUG
cout << "1 in set1" << endl;
#endif // DEBUG
for (it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
//cout << endl;
for (itr = s2.rbegin(); itr != s2.rend(); itr++)
{
cout << *itr << " ";
}
cout << endl;
}
cout << "The cost is: " << cost << endl;
return 0;
}
/*
test case:
4
1 1
1 5
4 5
4 9
4
1 1
2 2
3 3
4 3
*/
/*
0 1 3 2 0
18
0 1 2 3 0
The cost is: 7.43398
*/