-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuva-10034.cpp
More file actions
168 lines (134 loc) · 3.54 KB
/
uva-10034.cpp
File metadata and controls
168 lines (134 loc) · 3.54 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
#include <bits/stdc++.h>
using namespace std;
/* something starts */
/* something ends */
/* typedef starts */
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<double, int> pdi; /// double -> key(weight), int -> node
/* typedef ends */
/* macro starts */
#define PI acos(-1.0)
#define INF 0X7FFFFFFF
#define MAX_NODE 105
/* macro ends */
double matrix[MAX_NODE][MAX_NODE];
struct point
{
double x;
double y;
double d; /// distance from the reference point
};
struct compare
{
bool operator()(pdi &a, pdi &b)
{
return a.first > b.first;
}
};
double MSTPrim(int source, int nodes);
void printMatrix(int nodes);
int main()
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int test, t, n, i, j, _count;
double min_x, min_y, dx, dy, ans;
point p;
vector<point> points;
scanf("%d", &test);
for (t = 1; t <= test; t++) {
scanf("%d", &n);
points.clear();
min_x = min_y = INF;
p.d = 0.0;
for (i = 1; i <= n; i++) {
scanf("%lf%lf", &p.x, &p.y);
points.push_back(p);
//min_x = min(min_x, p.x);
//min_y = min(min_y, p.y);
}
//bool alreadyCalculated[MAX_NODE][MAX_NODE];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (i == j) {
matrix[i][j] = INF;
continue;
}
dx = abs(points[i].x - points[j].x);
dy = abs(points[i].y - points[j].y);
dx *= dx;
dy *= dy;
matrix[i][j] = sqrt(dx + dy);
}
}
//printMatrix(n);
if (t != 1) {
printf("\n");
}
printf("%.2lf\n", MSTPrim(0, n));
}
return 0;
}
double MSTPrim(int source, int nodes)
{
priority_queue<pdi, vector<pdi>, compare> PQ;
vector<bool> inMST(nodes, false);
vector<double> key(nodes, INF);
vector<int> parent(nodes, -1);
int _count = 0, u, v, last;
pdi p;
double ans = 0;
key[source] = 0;
PQ.push(make_pair(key[source], source));
while (!PQ.empty()) {
u = PQ.top().second;
PQ.pop();
inMST[u] = true;
//printf("u = %d\n", u);
_count++;
for (v = 0; v < nodes; v++) {
double w = matrix[u][v];
if (!inMST[v] && key[v] > w) {
key[v] = w;
PQ.push(make_pair(key[v], v));
parent[v] = u;
last = v;
//ans += w;
//printf("v = %d count = %d\n", v, _count);
//for (int k = 0; k < nodes; k++) {
//printf("key[%d] = %.2lf\n", k, key[k]);
//}
}
}
/*
if (_count == nodes) {
break;
}
*/
}
for (int i = 0; i < nodes; i++) {
ans += key[i];
}
return ans;
}
void printMatrix(int nodes)
{
cout << " \t";
for (int i = 0; i < nodes; i++) {
cout << i << "\t";
}
cout << "\n";
for (int i = 0; i < nodes; i++) {
cout << i << "\t";
for (int j = 0; j < nodes; j++) {
cout << matrix[i][j] << "\t";
}
cout << "\n";
}
cout << "\n";
}