-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartA.cpp
161 lines (145 loc) · 4.08 KB
/
partA.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
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
#include "PartA.h"
void readFile(vector<Pokemon> &graph) {
int numV;
cin >> numV;
graph.reserve(numV);
int x, y;
int numSea = 0, numCoast = 0, numLand = 0;
for (int i = 0; i < numV; i++) {
cin >> x >> y;
if (x < 0 && y < 0) {
numSea++;
}
else if ((x <= 0 && y == 0) || (x == 0 && y <= 0)) {
numCoast++;
}
else {
numLand++;
}
graph.push_back(Pokemon(x, y));
}
if (numSea > 0 && numLand > 0 && numCoast == 0) {
cerr << "Cannot construct MST\n";
exit(1);
}
}
double calcDistance(Pokemon &pokemon1, Pokemon &pokemon2) {
double x1 = (double)pokemon1.getX();
double y1 = (double)pokemon1.getY();
double x2 = (double)pokemon2.getX();
double y2 = (double)pokemon2.getY();
double distance;
distance = sqrt((double)((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
return distance;
}
//FIXME: REMOVE AFTER OPTIMIZING
/*
int findMinKey(vector<Pokemon> &graph) {
double min = numeric_limits<double>::max();
int minIndex = 0;
for (int i = 0; i < (int)graph.size(); i++) {
if (graph[i].isVisited() == false && graph[i].getDistance() < min) {
minIndex = i;
min = graph[i].getDistance();
}
}
return minIndex;
}
*/
//FIXME: OPTIMIZE
double findMST(vector<Pokemon> &graph) {
graph[0].setDistance(0);
graph[0].setPrev(-1);
double weight = 0;
int minEdgeIndex = 0;
for (int i = 0; i < (int)graph.size(); i++) {
double minDistance = numeric_limits<double>::max();
int minIndex;
//int numIndexChange = 0;
if (i == 0) {
minIndex = 0;
}
else {
minIndex = minEdgeIndex;
}
graph[minIndex].setVisited(true);
weight += graph[minIndex].getDistance();
double distance;
if (i != (int)graph.size() - 1) {
if (isSea(graph[minIndex])) {
for (int j = 0; j < (int)graph.size(); j++) {
if ((isSea(graph[j]) || isCoast(graph[j])) &&
graph[j].isVisited() == false &&
j != minIndex) {
distance = calcDistance(graph[minIndex], graph[j]);
if (distance < graph[j].getDistance()) {
graph[j].setDistance(distance);
graph[j].setPrev(minIndex);
}
}
if (graph[j].isVisited() == false &&
graph[j].getDistance() - minDistance < 0 &&
j != minIndex) {
minDistance = graph[j].getDistance();
minEdgeIndex = j;
}
}
}
else if (isLand(graph[minIndex])) {
for (int j = 0; j < (int)graph.size(); j++) {
if ((isLand(graph[j]) || isCoast(graph[j])) &&
graph[j].isVisited() == false &&
j != minIndex) {
distance = calcDistance(graph[minIndex], graph[j]);
if (distance < graph[j].getDistance()) {
graph[j].setDistance(distance);
graph[j].setPrev(minIndex);
}
}
if (graph[j].isVisited() == false &&
graph[j].getDistance() - minDistance < 0 &&
j != minIndex) {
minDistance = graph[j].getDistance();
minEdgeIndex = j;
}
}
}
else {
for (int j = 0; j < (int)graph.size(); j++) {
if (graph[j].isVisited() == false && j != minIndex) {
distance = calcDistance(graph[minIndex], graph[j]);
if (distance < graph[j].getDistance()) {
graph[j].setDistance(distance);
graph[j].setPrev(minIndex);
}
}
if (graph[j].isVisited() == false &&
graph[j].getDistance() - minDistance < 0 &&
j != minIndex) {
minDistance = graph[j].getDistance();
minEdgeIndex = j;
}
}
}
}
}
return weight;
}
void printMST(vector<Pokemon> &graph) {
for (int i = 1; i < (int)graph.size(); i++) {
cout << min(i, graph[i].getPrev()) << " "
<< max(i, graph[i].getPrev()) << "\n";
}
}
bool isSea(Pokemon &pokemon) {
return (pokemon.getX() < 0 && pokemon.getY() < 0);
}
bool isCoast(Pokemon &pokemon) {
return ((pokemon.getX() <= 0 && pokemon.getY() == 0) ||
(pokemon.getX() == 0 && pokemon.getY() <= 0));
}
bool isLand(Pokemon &pokemon) {
return ((pokemon.getX() > 0 && pokemon.getY() > 0) ||
(pokemon.getX() <= 0 && pokemon.getY() > 0) ||
(pokemon.getX() > 0 && pokemon.getY() <= 0));
}