This problem is seeking the minimum spanning tree.
Given the coordinates of the node, we need to calculate the distance based on these coordinates between the points.
In addition to this is the standard prime algorithm, the energy use prime basically, you can use Kruskal.
The classic algorithm must be filled in, proficiency level. Otherwise it is very difficult to exploit.
And the classic algorithm is classic. One of the reasons is not so easy to imagine themselves out of thin air, so be skilled.
#include <stdio.h> #include <string.h> #include <queue> #include <float.h> #include < Algorithm> #include <math.h>using namespace std;struct point{float x, y;}; const int max_n = 101; Point P[max_n];bool vis[max_n];float dist[max_n];float mindist[max_n];float caldist (Point &a, point &b) {float x = A.x-b.x;float y = A.y-b.y;return sqrtf (x*x + y*y);} void Prime (int n) {memset (Vis, 0, sizeof (BOOL) * (n+1)), vis[1] = true;dist[1] = 0;for (int i = 2; I <= n; i++) {Dist[i] = Caldist (P[1], p[i]);} for (int i = 1, i < n; i++) {Float mind = flt_max;int id = 0;for (int j = 2; J <= N; j + +) {if (!vis[j] && Dist [j] < mind) {mind = Dist[j];id = j;}} Vis[id] = True;mindist[i] = mind;for (int j = 2; J <= N; j + +) {if (!vis[j]) {float d = caldist (P[id], p[j]); if (D < di ST[J]) dist[j] = D;}}} int main () {int n;scanf ("%d", &n), for (int i = 1; I <= n; i++) {scanf ("%f%f", &p[i].x, &P[I].Y);} Prime (n); float ans = 0.f;for (int j = 1; j < N; J + +) {ans + = mindist[j];} printf ("%.2f\n", ans); return 0;}
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
POJ 2560 freckles Prime problem Solving algorithm