Outlets

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
 
 
 
/*
已经有一条边已经连好的情况下求最小生成数
并查集 Kruskal 
*/ 
#include <iostream>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

#define Max 52

struct Store
{
    int x, y;
};

struct Edge
{
    int start, end;
    
    double w;
};

int n, p, q, index, cnt, pqNum;        //n为点数 index为边数 cnt记录生成树的边数 pqNum记录pq对应的边的编号 
Store s[Max];
Edge edge[Max*Max];
int father[Max*Max];
double ans;

double Dis(Store a, Store b)
{
    return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
}

void build()
{
    index = 0;
    
    for (int i = 0; i < n; ++i)
    {
        for (int j = i+1; j < n; ++j)
        {
            edge[index].start = i;
            
            edge[index].end = j;
            
            edge[index++].w = Dis(s[i], s[j]);
        }
    }
}

bool cmp(Edge a, Edge b)
{
    return a.w <= b.w;
}

void findPQ()    //寻找pq对应哪一条边 
{
    for (int i = 0; i < index; ++i)
    {
        if ((edge[i].start == p-1 && edge[i].end == q-1)||(edge[i].start == q-1 && edge[i].end == p-1))//坑:边是记录单向的 
        {
            pqNum = i;
            
            return;
        }
    }
}

void init()
{
    cnt = 0;
    
    ans = 0;
    
    for (int i = 0; i < index; ++i)
    {
        father[i] = i;
    }
    
    sort(edge, edge+index, cmp);
    
    findPQ();
}

int find(int x)
{
    if (father[x] != x)
    {
        father[x] = find(father[x]);
    }
    
    return father[x];
}

void union_set(int i)
{
    int x = find(edge[i].start);
    
    int y = find(edge[i].end);

    if (x != y)
    {
        father[x] = y;
        
        cnt++;
        
        ans += edge[i].w;
    }
}

void Kruskal()
{
    init(); 
    
    union_set(pqNum);
    
    for (int i = 0; i < index; ++i)    //遍历每一条边 
    {
        if (i == pqNum)
            continue;
            
        if (cnt == n-1)    //若已经覆盖到全部点(n-1条边),则算法结束 
            return;
        else
            union_set(i); 
    }
}

void input()
{
    cin >> p >> q;
    
    for (int i = 0; i < n; ++i)
        cin >> s[i].x >> s[i].y;
}

int main()
{
    while (cin >> n, n)
    {    
        input(); 
        
        build();        //构图,将点位置记录转化为边的记录 
        
        Kruskal();         
        
        cout << fixed << setprecision(2) << ans << endl; 
    }
}
View Code

 

posted @ 2013-06-14 17:31  Norcy  阅读(413)  评论(0编辑  收藏  举报