HDU 1875 unblocked Project re-continuation (Kruskal algorithm)

Source: Internet
Author: User

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&LT;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", &AMP;A[I].X,&AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.