Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- using namespace std;
- int main() {
- int n;
- cin >> n;
- cout.precision(20);
- vector<pair<double, double> > points;
- vector<vector<double >> g;
- for (int i = 0; i < n; i++) {
- double x, y;
- cin >> x >> y;
- points.push_back(make_pair(x, y));
- }
- for (int i = 0; i < n; i++) {
- vector<double> tmp;
- for (int j = 0; j < n; j++) {
- if (j == i) {
- tmp.push_back(0);
- } else {
- tmp.push_back(sqrt(pow((points[i].first - points[j].first), 2) +
- pow((points[j].second - points[i].second), 2)));
- }
- }
- g.push_back(tmp);
- tmp.clear();
- }
- /*
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- cout << g[i][j] << " ";
- }
- cout << "\n";
- }
- */
- const int INF = 1000000000;
- vector<bool> used(n);
- vector<double> min_e(n, INF);
- vector<int> sel_e(n, -1);
- vector<pair<int, int>> res;
- double weight = 0;
- min_e[0] = 0;
- for (int i = 0; i < n; ++i) {
- int v = -1;
- for (int j = 0; j < n; ++j)
- if (!used[j] && (v == -1 || min_e[j] < min_e[v]))
- v = j;
- used[v] = true;
- if (sel_e[v] != -1) {
- weight += g[v][sel_e[v]];
- res.push_back(make_pair(v + 1, sel_e[v] + 1));
- }
- for (int to = 0; to < n; ++to)
- if (g[v][to] < min_e[to]) {
- min_e[to] = g[v][to];
- sel_e[to] = v;
- }
- }
- cout << weight << "\n";
- cout << n - 1 << "\n";
- for (auto el:res) {
- cout << el.first << " " << el.second << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement