Arctic Network---poj2349 minimum spanning tree

Source: Internet
Author: User

http://poj.org/problem?id=2349

Test instructions: There are n points given coordinates, between points and points can be radio or satellite communications, each point has a radio transceiver for radio communication, but only m points have satellite communication function. The distance of satellite communications can be infinitely large, but the distance of radio communication cannot exceed D, and the portion exceeding D will increase the cost of communication. To minimize the cost of communication.

In fact, is to seek a minimum spanning tree, m points have satellite communications, then there will be m-1 edge of the communication distance is infinite, in fact, this is the m-1 side without the cost calculation. The rest of the edges, find the maximum edge as the D value, so that all the remaining edges will not be greater than D, then no increase in communication costs. Save all the edge weights in the process of building the MST, then sort them in ascending order, removing the last m-1 (i.e. the largest m-1 edges, which use satellite communications), and the largest one is D.

The D array records the edges of the smallest spanning tree;

#include <stdio.h>#include<string.h>#include<map>#include<iostream>#include<algorithm>#include<math.h>#defineN 510#defineINF 0XFFFFFFFusing namespacestd;structnode{intx, y; DoubleD;} A[n],b[n*N];intCMP (node P,node q) {returnP.D <q.d;}intF[n];intFind (intx) {    if(x!=f[x]) f[x]=Find (f[x]); returnf[x];}intMain () {intT, M, N, I, J, K, p; Doubled[n*N]; scanf ("%d", &T);  while(t--) {scanf ("%d%d", &m, &N);  for(i=0; i<=n; i++) F[i]=i; Memset (A,0,sizeof(a)); memset (b,0,sizeof(b)); memset (d,0,sizeof(d));  for(i=1; i<=n; i++) scanf ("%d%d", &a[i].x, &a[i].y); K=0;  for(i=1; i<=n; i++)        {             for(j=1; j<i; J + +) {b[k].x=i; B[k].y=J; B[k++].D = sqrt ((a[i].x-a[j].x) * (a[i].x-a[j].x) + (A[I].Y-A[J].Y) * (a[i].y-a[j].y)); }} sort (b, B+K, CMP); P=0;  for(i=0; i<k; i++)        {            intPX =Find (b[i].x); intPY =Find (B[I].Y); if(px!=py) {F[PX]=py; D[p++] =B[I].D; }} printf ("%.2f\n", d[p-m]); }    return 0;}

Arctic Network---poj2349 minimum spanning tree

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.