Link: Click to open link
Test instructions:
There are N islands, giving the coordinates of each island, calculating its distance when the distance is greater than 10 or less than 1000 when the edge is taken away, each meter distance to build the bridge takes 100, for the minimum cost.
Problem Solving Ideas:
Enter the bridge coordinates first, then use two for loop to walk out of the nearest island from each island, the distance and i,j value into a structure, i,j as a label, and then you can use the Kruskal algorithm to continue to do.
Reference code:
#include <stdio.h> #include <math.h> #include <algorithm>using namespace Std;int per[110];struct Island{double x, y;} A[110];struct node{int u,v;double W;} B[10010];int CMP (node A,node b) {return A.W<B.W;} Double distance (double x1,double y1,double x2,double y2) {return sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (Y1-y2));} int find (int x) {int r=x;while (r!=per[r]) r=per[r];int j,i=x;while (i!=r) {j=per[i];p er[i]=r;i=j;} return r;} void join (int x,int y) {int fx=find (x); int fy=find (y); if (fx!=fy) {per[fx]=fy;}} int main () {int t;scanf ("%d", &t), while (t--) {int flag=0;int c,k=0;scanf ('%d ', &c); for (int i=1;i<=c;i++) {per [I]=i;scanf ("%lf%lf", &A[I].X,&A[I].Y);} for (int i=1;i<c;i++) {for (int j=i+1;j<=c;j++) {double dis=distance (A[I].X,A[I].Y,A[J].X,A[J].Y); if (dis>=10 &&dis<=1000) {b[k].w=dis;b[k].u=i;b[k++].v=j;}}} if (k<c-1) {printf ("oh!\n"); continue;} Sort (b,b+k,cmp);d ouble sum=0;for (int i=0;i<k;i++) {if (Find (B[I].U)!=find (B[I].V)) {join (B[I].U,B[I].V); sum+=b[i ].W;}} for (int i=1;i<=c;i++) if (per[i]==i) flag++;if (flag==1) printf ("%.1lf\n", sum*100); elseprintf ("oh!\n");} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 1875 unblocked Project re-continuation (Kruskal algorithm)