http://acm.hdu.edu.cn/showproblem.php?pid=1875
#include#include #include struct Pos { int x; int y; int operator- (const Pos& rhs) const { return (x - rhs.x) * (x - rhs.x) + (y - rhs.y) * (y - rhs.y); // use square value avoid to compare double type } }; static const int V = 100; Pos islands[V]; int G[V][V]; double Prim(int n) { int* dist = G[0]; // start from vertex0, use the first row of the matrix as the distance vector int c = n; double amount = 0.0; while (--c) { int add, min = INT_MAX; for (int i = 1; i < n; ++i) // find the non-connected vertex with the shortest the path { if (dist[i] != 0 && dist[i] < min) { add = i; min = dist[i]; } } if (min == INT_MAX) // all left vertices are isolated from the tree return -1; amount += sqrt((double)min); dist[add] = 0; // set to 0 present that this vertex has been connected to the tree for (int i = 1; i < n; ++i) // update the distance vector { if (G[add][i] < dist[i]) dist[i] = G[add][i]; } } return amount; } int main() { int t; scanf ("%d", &t); while (t--) { int c; scanf ("%d", &c); for (int i = 0; i < c; ++i) { scanf ("%d %d", &islands[i].x, &islands[i].y); G[i][i] = 0; for (int j = 0; j < i; ++j) { int d = islands[i] - islands[j]; if (d < 10 * 10 || d > 1000 * 1000) d = INT_MAX; G[i][j] = G[j][i] = d; } } double amount = Prim(c); if (amount == -1) printf ("oh!/n"); else printf ("%.1lf/n", amount * 100); } return 0; }