POJ 2349 Arctic Network minimum Spanning tree

Source: Internet
Author: User

The subject is also the smallest spanning tree that can be used with both Prime and Kruskal.

The point is that the biggest S-1 distances need to be removed because satellites can be used to cover these distances.

Tip: build a map, speed up, do not calculate on both sides.

The prime is used here because it is a dense map.


#include <stdio.h> #include <math.h> #include <stdlib.h> #include <string.h> #include < limits.h> #include <algorithm>using std::sort;const int max_vec = 501;struct pos{int x, y;}; float Gra[max_vec][max_vec];  Pos pos[max_vec];float dist[max_vec];bool vis[max_vec];int N, S, P;float Prime () {memset (Vis, 0, sizeof (BOOL) * P); Vis[0] = true;for (int i = 0; i < P-1; i++) {Float m = (float) int_max;int id = 0;for (int j = 0; J < P; J + +) {if (!vis[j] &amp ;& Gra[0][j] < m) {m = Gra[0][j];id = j;}} Vis[id] = True;dist[i] = m;for (int j = 0; J < ID; j + +) {if (!vis[j] && gra[0][j] > Gra[j][id]) gra[0][j] = g Ra[j][id];;} for (int j = id+1; J < P; J + +) {if (!vis[j] && gra[0][j] > Gra[id][j]) gra[0][j] = Gra[id][j];}} Sort (dist, dist+p-1); return dist[p-1-s];} int main () {scanf ("%d", &n), while (n--) {scanf ("%d%d", &s, &p), for (int i = 0; i < P; i++) {scanf ("%d%d", &A mp;pos[i].x, &AMP;POS[I].Y);} for (int i = 0; i < P; i++) {for(int j = i+1; J < P; J + +) {Float A = float (pos[i].x-pos[j].x), float B = float (POS[I].Y-POS[J].Y); Gra[i][j] = sqrtf (a*a + b*b);}} printf ("%.2f\n", Prime ());} return 0;}



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.