-
Notifications
You must be signed in to change notification settings - Fork 1
/
Frogger165.cpp
130 lines (128 loc) · 2.6 KB
/
Frogger165.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
#include <iostream>
#include <list>
#include <cstdlib>
#include <math.h>
#include <string.h>
#include <iomanip>
using namespace std;
int set[201];
class Edge {
public:
int u;
int v;
double w;
Edge(int from, int to, double weight);
Edge();
};
Edge::Edge() {
u = -1;
v = -1;
w = -1;
}
Edge::Edge(int from, int to, double weight) {
u = from;
v = to;
w = weight;
}
Edge* edges;
int compareEdge(const void* a, const void* b) {
Edge* e1 = (Edge*)a;
Edge* e2 = (Edge*)b;
if (e1->w > e2->w)
return 1;
else if (e2->w > e1->w)
return -1;
else
return 0;
}
int find(int element) {
if (set[element] < 0)
return element;
return set[element] = find(set[element]);
}
void UnionSet(int r1, int r2) {
set[r1] += set[r2];
set[r2] = r1;
}
void Union(int root1,int root2) {// calling this function only if find doesnt occu;
if (set[root1] < set[root2]) {//root1 has more members
UnionSet(root1, root2);
}
else {
UnionSet(root2, root1);
}
}
pair <int,int> stone[201];
double length = 0.0;
list<Edge>* MST;
int *visited;
bool flag1 = true;//
void dfs(int index, double l) {
list<Edge>::iterator it;
if (index == 2) {
length = l;
flag1 = false;
return;
}
visited[index] = true;
for (it = MST[index].begin(); it != MST[index].end(); it++) {
if (visited[it->v] == false &&flag1) {
double weight=l;
if (it->w > l)
weight = it->w;
dfs(it->v, weight);
}
}
}
int main() {
int n;
int program_counter = 1;
while (cin >> n) {
if (n == 0)
break;
length = 0.0;
flag1 = true;
MST = new list<Edge>[n + 1];
visited = new int[n + 1];
edges = new Edge[(n*(n - 1)) / 2 +1];
memset(&set, -1, sizeof(int)*(n + 1));
memset(visited, false, sizeof(int)*(n + 1));
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
stone[i] = make_pair(x, y);
}
int k = 0;
for (int i = 1; i <= n; i++) {
double dist;
for (int j = i + 1; j <= n; j++) {
dist= sqrt(pow(stone[j].first - stone[i].first,2)+ pow(stone[j].second - stone[i].second, 2));
Edge temp(i, j, dist);
edges[k++] = temp;
}
}
int ctr = 0;
qsort(edges, k, sizeof(Edge), compareEdge);
for (int i = 0; i < k; i++) {
int u = edges[i].u;
int v = edges[i].v;
double w = edges[i].w;
int root1 = find(u);
int root2 = find(v);
if (root1 == root2)
continue;
ctr++;
MST[u].push_back(edges[i]);
Edge temp(v, u, w);
MST[v].push_back(temp);
Union(root1, root2);
if(ctr==n-1)
break;
}
list<Edge>::iterator it;
dfs(1, 0);
cout << "Scenario #" << program_counter++ << endl;
cout << "Frog Distance = " << fixed << setprecision(3) << length << endl << endl;
}
return 0;
}