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
 
Source
 
Problem analysis:
This problem is about the MST(Minimal Spanning Tree) algorithm;
And the problem is not hard,so not much said;
Here post two kinds of solution to the problem:
Case 1:
View Code
#include<iostream>
#include<math.h>
using namespace std;
#define N 55
int x[N],y[N];
int f[N];
struct node 
{
    int num1;
    int num2;
    int  d;
    int flag;
}s[3000];
void sequence(int a)
{
    int i;
    for(i=1;i<=a;i++)
        f[i]=i;
}
int find(int X)
{
    while(X!=f[X])
    {
        X=f[X];
    }
    return  X;
}
int Union(int X, int Y)
{
    int flag;
    X=find(X);
    Y=find(Y);
    if(X!=Y)
    {
        flag=1;
        f[X]=Y;
    }
    else
        flag=0;
    return flag;
}
int cmp(const void* a,const void* b)
{
   struct node *c=(node *)a;
   struct node *d=(node *)b;
   if(c->flag!=d->flag)
       return d->flag - c->flag;
   else
       return c->d - d->d;
}
int main()
{
    int n;
    int i,j;
    while(cin>>n)
    {

        if(n==0) break;
        int p,q;
        cin>>p>>q;
        sequence(n);
        for(i=1;i<=n;i++)
        {
            cin>>x[i]>>y[i];
        }
        memset(s,0,sizeof(s));
        
        int t=0;
        for(i=1;i<n;i++)
            for(j=i+1;j<=n;j++)
            {
                 int dis=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
                 s[t].d=dis;
                 s[t].num1=i;
                 s[t].num2=j;
                 if(s[t].num1==p&&s[t].num2==q)
                     s[t].flag=1;
                 if(s[t].num1==q&&s[t].num2==p)
                     s[t].flag=1;
                   t++;
            }
        qsort(s,t,sizeof(s[0]),cmp);
        //for(i=0;i<t;i++)
        //{
        //    printf("%d %d %d %d\n",s[i].num1,s[i].num2,s[i].flag,s[i].d);
        //}
        double sum=0;
        int ans;
        for(i=0;i<t;i++)
        {
            ans=Union(s[i].num1,s[i].num2);
            if(ans)
                sum+=sqrt((double)s[i].d);
        }
        printf("%.2lf\n",sum);
    }
    return 0;
}
            


                 

        

The solution to the problem is not good, next will see a better one;

Case 2:

View Code
#include<iostream>
#include<math.h>
using namespace std;
#define N 55
int x[N],y[N];
int f[N];
void sequence(int a)
{
    int i;
    for(i=1;i<=a;i++)
        f[i]=i;
}
int find(int x)
{
    while(x!=f[x])
    {
        x=f[x];
    }
    return x;
}
int Union(int x,int y)
{
    int flag;
    x=find(x);
    y=find(y);
    if(x!=y)
    {
        flag=1;
        f[x]=y;
    }

    else
        flag=0;
    return flag;
}
struct node
{
    int num1,num2,d;
}s[N*N];
int cmp(const void*a,const void*b)
{
    struct node*c=(node *)a;
    struct node*d=(node *)b;
    return c->d-d->d;

}
int main()
{
    int n;
    int i,j;
    while(cin>>n)
    {
        if(n==0) break;
        sequence(n);
        int p,q;
        cin>>p>>q;
        for(i=1;i<=n;i++)
        {
            cin>>x[i]>>y[i];
        }
        int t=0;
        for(i=1;i<n;i++)
            for(j=i+1;j<=n;j++)
            {
                s[t].num1=i;
                s[t].num2=j;
                s[t].d=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
                t++;
            }
        double sum=sqrt(1.0*((x[p]-x[q])*(x[p]-x[q])+(y[p]-y[q])*(y[p]-y[q])));
        qsort(s,t,sizeof(s[0]),cmp);
        int k=Union(p,q);
        int ans;
        for(i=0;i<t;i++)
        {
            ans=Union(s[i].num1,s[i].num2);
            if(ans)
                sum+=sqrt((double)s[i].d);
        }
        printf("%.2lf\n",sum);
    }
    return 0;
}

Of course, the second code is not  concise.And  I need perfect it when I am free, but you konw I am very busy these days.

转载于:https://www.cnblogs.com/heat-man/articles/2762439.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值