HDU 4463 Outlets


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
 

        一个简单的模板题,prim,kruskal都可以解决,关键在于如何把点的坐标处理成两点之间的距离,我采用的是kruskal算法,先把全部的点存到数组中,在进行计算每两个点之间的距离,存到结构体中,本人认为这个题不好,不光是后台数据水,更重要的是若后台数据不水的话难度估计会上升好几个档次。

比如    4 

           1  4

           -2 0

           -1 0

            0  0

            1  0

  这组 数据正常来说 直接就可以输出3  但是按prim还是kruskal都不是,输出的是5.

如果自己想尝试按3做的话,可以试试.

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
int f[110];
struct node
{
    int x,y;
    double z;
} map[2510];
bool cmp (node A,node B)
{
    return A.z<B.z;
}
int find(int x)
{
    if(f[x]==x)
        return x;
    else return f[x]=find(f[x]);
}
int Union (int x,int y)
{
    int tx=find(x);
    int ty=find(y);
    if(tx!=ty)
    {
        f[tx]=ty;
        return 1;
    }
    else return 0;
}
int main()
{
    int i,j,n,m,t;
    int a[110],b[110];
    int p,q;
    while(scanf("%d",&n)&&n)
    {
        scanf("%d%d",&p,&q);
        for(i=1; i<=n; i++)
            scanf("%d%d",&a[i],&b[i]);
        for(i=0; i<=n; i++)
            f[i]=i;
        int num=0,k=0;
        double sum=0;
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                map[k].x=i;
                map[k].y=j;
                map[k].z=sqrt((a[i]-a[j])*(a[i]-a[j])*1.0+(b[i]-b[j])*(b[i]-b[j])*1.0);
                if(i==p&&j==q)
                {
                    Union(p,q);
                    num++;
                    sum+=map[k].z;
                }
                k++;
            }
        sort(map,map+k,cmp);
        for(i=1; i<k; i++)
        {
            if(Union(map[i].x,map[i].y))
            {
                sum+=map[i].z;
                num++;
            }
            if(num==n-1)
                break;
        }
        printf("%.2lf\n",sum);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值