POJ2349 + Prim

Source: Internet
Author: User

/* Prim: given some points, a satellite can connect two points, and there is a certain distance between points and points. Ask the minimum distance that can connect all points. */# Include <stdio. h> # include <string. h> # include <stdlib. h> # include <algorithm> # include <iostream> # include <queue> # include <map> # include <stack> # include <set> # include <math. h> using namespace std; typedef long int64; // typedef _ int64 int64; typedef pair <int64, int64> PII; # define MP (a, B) make_pair (a), (B) const int maxn = 505; const int inf = 0x7fffffff; const double pi = acos (-1.0); const double eps = 1e-8; struct Node {double x, y;} a [maxn]; int vis [maxn]; double dis [maxn]; double mat [maxn] [maxn]; double dist (Node a, Node B) {return sqrt (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} void init (int n) {for (int I = 1; I <= n; I ++) {for (int j = 1; j <= n; j ++) {mat [I] [j] = dist (a [I], a [j]) ;}} return ;} void prim (int n) {for (int I = 1; I <= n; I ++) {vis [I] = 0; dis [I] = mat [1] [I];} vis [1] = 1; for (int I = 1; I <= n; I ++) {int M = inf; int id =-1; for (int j = 1; j <= n; j ++) {if (vis [j] = 0 & M> dis [j]) {M = dis [j]; id = j ;}} if (id =-1) return; vis [id] = 1; for (int j = 1; j <= n; j ++) {if (vis [j] = 0 & dis [j]> mat [id] [j]) {dis [j] = mat [id] [j] ;}}return ;}int main () {int T; scanf ("% d", & T ); while (T --) {int s, n; scanf ("% d", & s, & n); for (int I = 1; I <= n; I ++) scanf ("% lf", & a [I]. x, & a [I]. y); init (n); prim (n); sort (dis + 1, dis + 1 + n); // for (int I = 1; I <= n; I ++) // printf ("% lf", dis [I]); // puts (""); printf ("%. 2lf \ n ", dis [n-s + 1]);} return 0 ;}

 

Related Article

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.