Poj2349:arctic Network (minimum spanning tree)

Source: Internet
Author: User
Tags time limit
Total time limit: 2000ms memory limit: 65536kB Description The Department of National Defence (DND) wishes to connect several northern by A wireless network. Two different communication technologies are to is used in establishing the Network:every outpost would have a radio trans Ceiver and some outposts would in addition have a satellite channel.
Any two outposts with a satellite channel can communicate via the satellite, regardless of their location. Otherwise, two outposts can communicate by radio only if the distance between them does not exceed D, which depends of the Power of the transceivers. Higher power yields higher D but costs. Due to purchasing and maintenance considerations, the transceivers at the outposts must is identical; That's, the value of D is the same for every pair of outposts.

Your job is to determine the minimum D required for the transceivers. There must be at least one communication path (direct or indirect) between every pair of outposts. Enter the number of input contains N, the number of test cases. The "a" of each test case contains 1 <= s <=, the number of satellite channels, and S < P <=, t He number of outposts. P lines follow, giving the x,y coordinates of each outpost in km (coordinates are integers between 0 and 10,000). Output for each case, the output should consist of a single line giving the minimum D required to connect the network. Output should is specified to 2 decimal points. Sample input
1
2 4
0
0
0 + 150 750
Sample output
212.13
The question is a bit difficult to understand, the problem is very simple.

The point: There are n points to give coordinates, points and points between radio or satellite communications, each point has a radio transceiver can be used for radio communication, but only M point has satellite communication function. The distance of satellite communication can be unlimited, 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 communications.

In fact, is to seek a minimum spanning tree, M Point has satellite communications, then there will be m-1 edge of the communication distance is infinite, in fact, this is the m-1 edge without calculating costs. And the rest of the edges, find the maximum edge as the D value, so that all the remaining edges are not greater than D, then no increase in communication costs. Save all of the edge weights in the process of constructing the MST and then sort them in ascending order, removing the last m-1 (i.e. the largest m-1 edge, which uses satellite communications), and the biggest one is D; with the AC code:

#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> using namespace
Std
	struct node {int start;
	int end;
Double cost;
}T[1001000];
int CMP (node A,node b) {return a.cost <b.cost} int per[1100];
	int find (int x) {int r=x;
	while (R!=per[r]) r=per[r];
return R;
	int join (int x,int y) {int fx=find (x);
	int Fy=find (y);
		if (fx!=fy) {per[fx]=fy;
	return 1;
return 0;
	int main () {int k,m,n,p,i,j;
	int x[110010],y[110010];
	scanf ("%d", &p);
		while (p--) {for (i=0;i<1100;i++) per[i]=i;
		scanf ("%d%d", &m,&n);
		for (i=1;i<=n;i++) scanf ("%d%d", &x[i],&y[i));
		k=0;
			for (i=1;i<=n;i++) for (j=i+1;j<=n;j++) {t[k].start=i;
			T[k].end =j;
			T[k].cost =sqrt (1.0* (X[I]-X[J)) * (X[i]-x[j]) +1.0* (Y[I]-Y[J)) * (Y[I]-Y[J));
		k++;
		Sort (t,t+k,cmp);
		int v=0;
		Double ans;
			for (i=0;i<k;i++) {if (Join (t[i].start,t[i].end)) v++;
				if (v==n-m) {ans=t[i].cost;
	Break		} printf ("%.2f\n", ans);
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.