hdu 4463 Outlets【最小生成树】水题

Outlets

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3476    Accepted Submission(s): 1609

Problem Description

In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.

Input

There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.

Output

For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.

Sample Input

4

2 3

0 0

1 0

0 -1

1 -1

0

Sample Output

3.41

Source

2012 Asia Hangzhou Regional Contest

Recommend

We have carefully selected several similar problems for you:  5906 5905 5903 5902 5901 

 

 

题目大意:

一共有n个点。让你将这n个点都用路相连接起来 ,求一个最小花费。

第一行输入一个数n,表示点的个数。

第二行输入两个数a,b表示必须要用路相连接的两个点(直接相连的一条路)。

接下来n行,代表每个点的坐标。


思路:


1、将所有点都用路相连接起来,求一个最小花费,明显是一个最小生成树的问题,其说有两个点是必须要用直接相连的路连接的,那么预处理的时候将这个两个点提前合并起来即可。


2、O(n^2)求出两点间距离,然后克鲁斯卡尔算法将边贪心入树即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int x,y;
    double w;
}a[1515151];
int x[5000];
int y[5000];
int f[5000];
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int cmp(node a,node b)
{
    return a.w<b.w;
}
double Dis(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        int lu,lv;
        for(int i=1;i<=n;i++)f[i]=i;
        scanf("%d%d",&lu,&lv);
        merge(lu,lv);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&x[i],&y[i]);
        }
        int cont=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                a[cont].x=i;
                a[cont].y=j;
                a[cont++].w=Dis(i,j);
            }
        }
        double ans=0;
        sort(a,a+cont,cmp);
        for(int i=0;i<cont;i++)
        {
            if(find(a[i].x)!=find(a[i].y))
            {
                merge(a[i].x,a[i].y);
                ans+=a[i].w;
            }
        }
        printf("%.2lf\n",ans+Dis(lu,lv));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值