zoukankan      html  css  js  c++  java
  • 北极网络

    题目:Arctic Network

    网址:http://noi-test.zzstep.com/contest/0x6B「图论」练习/POJ2349 Arctic Network

    描述

    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.

    输入

    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)).

    输出

    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.

    样例输入

    1
    2 4
    0 100
    0 300
    0 600
    150 750
    

    样例输出

    212.13
    

    来源

    Waterloo local 2002.09.28


    (P)个结点想成(P)个联通块,按(Kruskal)算法,每次连边都将合并两个联通块。最后剩下S个联通块每个分配一台卫星即可。

    C ++ AC代码

    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<vector>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const int SIZE = 500 + 5;
    struct Edge
    {
    	int u, v;
    	double d;
    	bool operator <(const Edge& rhs)
    	{
    		return d < rhs.d;
    	}
    };
    vector <Edge> e;
    pair <int, int> p[SIZE];
    int n, S, P, fa[SIZE];
    double dis(int u, int v)
    {
    	return sqrt((p[u].first - p[v].first) * (p[u].first - p[v].first) + (p[u].second - p[v].second) * (p[u].second - p[v].second));
    }
    int get(int x)
    {
    	if(fa[x] == x) return x;
    	return fa[x] = get(fa[x]);
    }
    void Kruskal()
    {
    	for(int i = 1; i <= P; ++ i) fa[i] = i;
    	double ans = 0.0;
    	if(P <= S) puts("0.00");
    	else
    	{
    		for(int i = 0; i < e.size(); ++ i)
    		{
    			int u = e[i].u, v = e[i].v, v1 = get(u), v2 = get(v);
    			if(v1 == v2) continue;
    			ans = e[i].d;
    			-- P;
    			fa[v1] = v2;
    			if(P == S) break;
    		}
    		printf("%.2f
    ", ans);
    	}
    	return;
    }
    int main()
    {
    	scanf("%d", &n);
    	while(n --)
    	{
    		e.clear();
    		scanf("%d %d", &S, &P);
    		for(int i = 1; i <= P; ++ i)
    		{
    			scanf("%d %d", &p[i].first, &p[i].second);
    		}
    		Edge next;
    		for(int i = 1; i < P; ++ i)
    		{
    			for(int j = i + 1; j <= P; ++ j)
    			{
    				next.u = i, next.v = j;
    				next.d = dis(i, j);
    				e.push_back(next);
    			}
    		}
    		sort(e.begin(), e.end());
    		Kruskal();
    	}
    	return 0;
    }
    

    总结回顾

    这道题实在令我意外,对最小生成树的理解更深了一步。最小生成树实质上是合并不同的联通块,而不仅仅是连边。

    参考文献

  • 相关阅读:
    Linux基本命令
    IDEA实用插件
    Windows常用快捷键
    IDEA常用快捷键
    OOP三大特性之多态
    IDEA里配置SSM框架,配置没问题却报404错误
    Tomcat的80端口被占用问题_解决方案
    基于SpringBoot开发
    java数据结构--线性表
    代码优化设计(一)
  • 原文地址:https://www.cnblogs.com/zach20040914/p/13510470.html
Copyright © 2011-2022 走看看