H - 最小生成树模板+1(适合Prim) UVA - 10369

H - 最小生成树模板+1(适合Prim)

 UVA - 10369 

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 1 2 4 0 100 0 300 0 600 150 750 Sample Output 212.13

题意:有m个卫星,n个点,直接连接两点要买个道具,道具不同型号能连接的最大距离不同,道具的价格能连接的最大距离,用卫星连没有花费,为了规范,所买的道具都是同一型号的,问要连通所有点,最小买什么型号的道具

思路:最小生成树里减去m条最大的边,剩下的就是答案

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#define Twhile() int T;scanf("%d",&T);while(T--)
#define ArrInit(a,b,n) for(int i=0;i<=n;i++)a[i]=b
#define ArrInit2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b
#define fora(i,a,b) for(int i=a;i<b;i++)
#define fors(i,a,b) for(int i=a;i>b;i--)
#define fora2(i,a,b) for(int i=a;i<=b;i++)
#define fors2(i,a,b) for(int i=a;i>=b;i--)
#define PI acos(-1.0)
#define eps 1e-6
#define INF 0x3f3f3f3f

typedef long long LL;
typedef long long LD;
using namespace std;
const int maxn=500+11;
struct node
{
    int x,y;
}a[maxn];
double ma[maxn][maxn];//邻接矩阵
int N,M;//点数
//int ans;//最小生成树的权值之和
//stack<int>S;  //最小生成树加入的点的顺序
//int lowerPoint[maxn]; // i离S中的lowerPoint[i]最近
double lowerCost[maxn];//lowerCost离S的最小距离
set<double>treeEg;//生成树的边排序
int use[maxn];//use是否被加入S
void init()
{
    //ans=0;
    //ArrInit2(ma,INF,N,N);
    ArrInit(use,0,N);
}
void primInit()
{
//    S.push(1);
    treeEg.clear();
    use[1]=1;
    lowerCost[1]=0;
//    lowerPoint[1]=1;
    fora2(i,2,N)
    {
//        lowerPoint[i]=1;
        lowerCost[i]=ma[1][i];
    }
}
void prim()
{
    //printf("最小生成树:\n");
    primInit();
    int n=N-1;
    while(n--){
    int k=-1;
    double tem=INF;
    fora2(i,2,N)//找到距离S集合最小的点
    {
        if(use[i])continue;
        if(lowerCost[i]<tem)
        {
            tem=lowerCost[i];
            k=i;
        }
    }
    use[k]=1;
    //ans+=tem;
    treeEg.insert(tem);
    //printf("点%d 边权值:%f\n",k,tem);
    /*
    lowerCost[k]=0;lowerPoint[i]=i;  //不更新的话,当要求输出最小生成树的边可以直接输出
    S.push(k);
    */
    fora2(i,1,N)
    {
        if(use[i])continue;
        double t=ma[i][k];
        if(t<lowerCost[i])
        {
            lowerCost[i]=t;
            //lowerPoint[i]=k;
        }
    }
    if(tem==INF)return;//图不连通
    }
}

int main()
{
    Twhile()
    {
        scanf("%d%d",&M,&N);
        init();
        fora2(i,1,N)scanf("%d%d",&a[i].x,&a[i].y);
        fora2(i,1,N)fora2(j,1,i)
        {
            ma[i][j]=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
            ma[j][i]=ma[i][j];
        }
        //if(N==1){printf("0\n");continue;}
        prim();
        set<double>::iterator it=treeEg.end();
        while(M--)it--;
        printf("%.2f\n",*it);
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/107acm/p/9437905.html