HDU 4463 Outlets(一條邊固定的最小產生樹)

來源:互聯網
上載者:User

標籤:

Outlets

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


Problem DescriptionIn 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. 

 

InputThere 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. 

 

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

 

Sample Input42 30 01 00 -1 1 -10 

 

Sample Output3.41 

題目大意:

  這道題是說,給你n個點,然後要求p和q號城市之間必須有道路相串連,然後,其他的道路之間滿足最小產生樹。說白了,就是一個 一條邊固定的最小產生樹問題。

解題思路:

  直接用並查集搞就行了,沒有什麼難度。。。

代碼:

  

# include<cstdio># include<iostream># include<cmath># include<cstring># include<algorithm>using namespace std;# define MAX 54*54struct node{    int x,y;}point[MAX];int f[MAX];void init(){    for ( int i = 0;i < MAX;i++ )    {        f[i] = i;    }}struct edge{    int u,v;    double cost;}e[MAX];int cmp( const struct edge & a,const struct edge & b ){    return a.cost < b.cost;}double dis ( node a,node b ){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );}int getf( int x ){    if ( f[x]==x )        return x;    else    {        int t = getf(f[x]);        f[x] = t;        return f[x];    }}int main(void){    int n;    while ( scanf("%d",&n)!=EOF )    {        if ( n==0 )            break;        init();        int p,q; scanf("%d%d",&p,&q);        for ( int i = 1;i <= n;i++ )        {            scanf("%d%d",&point[i].x,&point[i].y);        }        int cnt = 0;        for ( int i = 1;i <= n-1;i++ )        {            for ( int j = i+1;j <= n;j++ )            {                if( i==p&&j==q||i==q&&j==p )                {                    e[cnt].cost = 0;                }                else                {                    e[cnt].cost = dis(point[i],point[j]);                }                e[cnt].u = i; e[cnt].v = j;                cnt++;            }        }        int num = 0;        double ans = 0;        sort(e,e+cnt,cmp);        ans = dis(point[p],point[q]);        for ( int i = 0;i < cnt;i++ )        {            int x = getf(e[i].u);            int y = getf(e[i].v);            if ( x!=y )            {                num++;                f[x] = y;                ans+=e[i].cost;                if(num==n-1)                    break;            }        }        printf("%.2lf\n",ans);        memset(e,0,sizeof(e));    }    return 0;}

  

HDU 4463 Outlets(一條邊固定的最小產生樹)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.