-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtoss
68 lines (59 loc) · 1.49 KB
/
toss
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
#include <bits/stdc++.h>
using namespace std;
int maxindex(int* dist, int n)
{
int mi = 0;
for (int i = 0; i < n; i++) {
if (dist[i] > dist[mi])
mi = i;
}
return mi;
}
void selectKcities(int n, int weights[4][4], int k)
{
int* dist = new int[n];
vector<int> centers;
for (int i = 0; i < n; i++) {
dist[i] = INT_MAX;
}
// index of city having the
// maximum distance to it's
// closest center
int max = 0;
for (int i = 0; i < k; i++) {
centers.push_back(max);
for (int j = 0; j < n; j++) {
// updating the distance
// of the cities to their
// closest centers
dist[j] = min(dist[j], weights[max][j]);
}
// updating the index of the
// city with the maximum
// distance to it's closest center
max = maxindex(dist, n);
}
// Printing the maximum distance
// of a city to a center
// that is our answer
cout << endl << dist[max] << endl;
// Printing the cities that
// were chosen to be made
// centers
for (int i = 0; i < centers.size(); i++) {
cout << centers[i] << " ";
}
cout << endl;
}
// Driver Code
int main()
{
int n = 4;
int weights[4][4] = { { 0, 4, 8, 5 },
{ 4, 0, 10, 7 },
{ 8, 10, 0, 9 },
{ 5, 7, 9, 0 } };
int k = 2;
// Function Call
selectKcities(n, weights, k);
}