POJ-2349 Arctic Network---The long edge of the MST

Source: Internet
Author: User

Topic Links:

https://vjudge.net/problem/POJ-2349

Main topic:

To establish a communication network between n nodes, where m nodes can be directly connected by satellite, and the remaining nodes are connected by line, to find out how long the maximum length of these lines will take

Ideas:

or the bare topic of MST, because there are m nodes can be connected by satellite, so after the MST, the longest m-1 edge can be connected with a satellite, just require the first m long edge, directly with the prim algorithm, save the length of each edge of MST, sorted directly after the output of the long edge.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <cmath>6#include <queue>7#include <stack>8#include <map>9#include <Set>Ten#include <sstream> One using namespacestd; AtypedefLong Longll; -typedef pair<int,int>Pair; - Const intMAXN = 1e3 +Ten; the Const intINF =1<< -; - intdir[4][2] = {1,0,0,1,-1,0,0,-1}; - intT, N, M; - DoubleMAP[MAXN][MAXN];//Save Diagram + DoubleLOWCOST[MAXN], ANS[MAXN]; - intMST[MAXN], tot; + Pair A[MAXN]; A voidPrimintU//minimum spanning tree start at { -      for(inti =1; I <= N; i++)//Initialize two arrays -     { -Lowcost[i] =Map[u][i]; -Mst[i] =u; -     } inMst[u] =-1;//set to 1 to indicate that the MST has been added -      for(inti =1; I <= N; i++) to     { +         DoubleMinn =INF; -         intv =-1; the         //find the minimum value for the lowcost array that is not included in MST *          for(intj =1; J <= N; J + +) $         {Panax Notoginseng             if(Mst[j]! =-1&& Lowcost[j] <Minn) -             { thev =J; +Minn =Lowcost[j]; A             } the         } +         if(V! =-1)//V=-1 indicates that the smallest edge is not found, -{//v indicates the shortest point of the current distance to MST $             //printf ("%d%d%d\n", Mst[v], V, Lowcost[v]);//Output Path $ans[tot++] =Lowcost[v]; -MST[V] =-1; -              for(intj =1; J <= N; J + +)//update the shortest edge the             { -                 if(Mst[j]! =-1&& Lowcost[j] >Map[v][j])Wuyi                 { theLOWCOST[J] =Map[v][j]; -MST[J] =v; Wu                 } -             } About         } $     } -     //printf ("Weight of MST is%d\n", sum_mst); -Sort (ans, ans +tot); -printf"%.2f\n", Ans[n-m-1]);//outputs the side of the M-long A } + intMain () the { -CIN >>T; $      while(t--) the     { theCIN >> M >>N; the          for(inti =1; I <= N; i++) Cin >> A[i].first >>A[i].second; the          for(inti =1; I <= N; i++) -         { in              for(intj =1; J <= N; J + +) theMAP[I][J] = map[j][i] = sqrt ((a[i].first-a[j].first) * (A[i].first-a[j].first) + (a[i].second-a[j].second) * (A[I].S Econd-a[j].second)); the         } Abouttot =0; thePrim1); the     } the     return 0; +}

POJ-2349 Arctic Network---The long edge of the MST

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.