专题测试四 图论 K - Arctic Network

  1. 题目
    The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication technologies are to be used in establishing the network: every outpost will have a radio transceiver and some outposts will 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 more. Due to purchasing and maintenance considerations, the transceivers at the outposts must be identical; that is, 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.
    Input
    The first line of input contains N, the number of test cases. The first line of each test case contains 1 <= S <= 100, the number of satellite channels, and S < P <= 500, the 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, output should consist of a single line giving the minimum D required to connect the network. Output should be specified to 2 decimal points.
    Sample
    Input Output
    1
    2 4
    0 100
    0 300
    0 600
    150 750
    
    212.13
    
  2. 思路
    最短的距离D就是最小生成树里留下的最长一条边,也就是Kruskal算法里面加入的第p-1条边,但是由于有卫星频道,可以无视两个频道间距离通信,因此有s个卫星频道就可以省去s-1条最长边(一个卫星频道显然没啥*用),在跑kruskal的时候只找到第p-s条边输出就可以了
  3. 代码
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    using namespace std;
    
    int fa[505];
    int s,p;
    double map[505][2];
    
    struct edge
    {
    	int a,b;
    	double w;
    	bool operator>(const edge& a) const { return w > a.w; }
    };
    
    int find(int x)
    {
    	if (fa[x] != x)
    		fa[x] = find(fa[x]);
    	return fa[x];
    }
    
    int build(int a,int b)
    {
    	int aa = find(a);
    	int bb = find(b);
    	if(aa!=bb)
    	{
    		fa[aa] = bb;
    		return 1;
    	}
    	return 0;
    }
    
    double dis(int i,int j)
    {
    	return sqrt((map[i][0] - map[j][0])*(map[i][0] - map[j][0]) + (map[i][1] - map[j][1])*(map[i][1] - map[j][1]));
    }
    
    int main()
    {
    	int n;
    	scanf("%d",&n);
    	while(n--)
    	{
    		scanf("%d%d",&s,&p);
    		priority_queue<edge,vector<edge>,greater<edge>>Q;
    		for(int i=1;i<=p;i++)
    		{
    			fa[i] = i;
    			scanf("%lf%lf",&map[i][0],&map[i][1]);
    		}
    		for(int i=1;i<=p;i++)
    		{
    			for(int j=i+1;j<=p;j++)
    			{
    				double w = dis(i,j);
    				edge ed;
    				ed.a = i, ed.b = j, ed.w = w;
    				Q.push(ed);
    			}
    		}
    		int num = 0;
    		while(!Q.empty())
    		{
    			edge ed = Q.top();
    			Q.pop();
    			if(build(ed.a, ed.b)) num++;
    			if(num == p-s)
    			{
    				printf("%.2f\n",ed.w);
    				break;
    			}
    		}
    	}
    	return 0;
    }
posted @ 2022-02-21 15:13  Benincasa  阅读(39)  评论(0编辑  收藏  举报