hdu 4463 Outlets

 Outlets

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


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算法

1、生成树的概念 
   连通图G的一个子图如果是一棵包含G的所有顶点的树,则该子图称为G的生成树。

  生成树是连通图的极小连通子图。所谓极小是指:若在树中任意增加一条边,则将出现一个回路;若去掉一条边,将会使之变成非连通图。 生成树各边的权值总和称为生成树的权。权最小的生成树称为最小生成树。

2、最小生成树的性质
   用哲学的观点来说,每个事物都有自己特有的性质,那么图的最小生成树也是不例外的。按照生成树的定义,n 个顶点的连通网络的生成树有 n 个顶点、n-1 条边。

3、构造最小生成树,要解决以下两个问题:
( 1).尽可能选取权值小的边,但不能构成回路(也就是环)。
(2).选取n-1条恰当的边以连接网的 n个顶点。

求最小生成树的算法一般都使用贪心策略,有Prim算法和Krusal算法等。

普里姆算法的基本思想:
1)清空生成树,任取一个顶点加入生成树;
2)在那些一个端点在生成树里,另一个端点不在生成树里的边中,选取一条权最小的边,将它和另一个端点加进生成树;
3)重复步骤2,直到所有的顶点都进入了生成树为止,此时的生成树就是最小生成树。

#include <stdio.h> 
#include<math.h> 
#include<string.h>
#include<stdlib.h>
#define N 55
#define Max 10000000
int mark[N];
double dis[N],min,s;
int n,p,q;
struct std
{
    double x,y;
}G[N];
double disp(double x1,double y1,double x2,double y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double Min(double a,double b)
{
    return a<b?a:b;
}
void prim()
{
    int i;                 //p在队列
    while(1)
    {
        min=Max;
        int t=-1;
        for(i=0;i<n;i++)
            if(!mark[i]&&dis[i]<min)
            {
                min=dis[i];
                t=i;
            }
        if(t==-1)
            break;
        mark[t]=1;
        s+=dis[t];
        for(i=0;i<n;i++)
            if(!mark[i])
                dis[i]=Min(dis[i],disp(G[i].x,G[i].y,G[t].x,G[t].y));
    }
}
int main() 
{ 
    int i;
    while(scanf("%d",&n),n)
    {
        for(i=0;i<n;i++)         //初始化
        {
            dis[i]=Max;
            mark[i]=0;
        }
        scanf("%d%d",&p,&q);
        p--;q--;
        for(i=0;i<n;i++)
            scanf("%lf%lf",&G[i].x,&G[i].y);
        dis[p]=disp(G[q].x,G[q].y,G[p].x,G[p].y);
        s=dis[p];                                    
        dis[q]=0;
        mark[p]=1;   
        for(i=0;i<n;i++)
            if(!mark[i])
                dis[i]=Min(dis[i],disp(G[i].x,G[i].y,G[p].x,G[p].y));
                               
        prim();
        printf("%.2f\n",s);
    }
    return 0; 
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值