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

思路:

  • 并查集和最小生成树的应用,只要在构建最小生成树的过程中,把题目中得到的p和q值对应的距离置为0,最后再加上即可。

代码:

 1 #include <iostream>
 2 #include <cstdio> 
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <cstring>
 6 using namespace std;
 7 
 8 int n;
 9 int pre[55];
10 double x[200], y[200];
11 
12 struct node{
13     int begin, end;
14     double len;
15 }s[50 * 50];
16 
17 bool cmp(node a, node b)
18 {
19     return a.len < b.len;
20 }
21 
22 void init(int n)
23 {
24     for(int i = 1; i <= n; i++)
25     {
26         pre[i] = i;
27     }
28 }
29 
30 int find(int x)
31 {
32     if(x != pre[x])
33         pre[x] = find(pre[x]);
34     return pre[x];
35 }
36 
37 void kruskal(int cnt, double ans)
38 {
39     double sum = 0;
40 //    int cntt = 0;
41     sort(s, s + cnt, cmp);
42     for(int i = 0; i < cnt; i++)
43     {
44         int fx = find(s[i].begin);
45         int fy = find(s[i].end);
46         if(fx != fy)
47         {
48             pre[fx] = fy;
49             sum += s[i].len;
50         }
51     }
52 //    printf("sum = %.2lf\n", sum);
53     printf("%.2lf\n", sum + ans); 
54 }
55 
56 int main()
57 {
58     int p, q;
59     while(~scanf("%d", &n) && n)
60     {
61         init(n);
62         scanf("%d%d", &p, &q);
63         for(int i = 1; i <= n; i++)
64         {
65             scanf("%lf%lf", &x[i], &y[i]);
66         }
67         int cnt = 0;
68         double ans = 0;        
69         for(int i = 1; i <= n; i++)    //建图 
70         {
71             for(int j = 1; j <= n; j++)
72             {    
73                 if((i == p && j == q) || (i == q && j == p))
74                 {
75                     s[cnt].begin = i;
76                     s[cnt].end = j;        
77                     s[cnt].len = 0;
78                     cnt++;
79                     ans = sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2));
80                 }    
81                 else
82                 {
83                     s[cnt].begin = i;
84                     s[cnt].end = j;        
85                     s[cnt].len = sqrt(pow(x[i] - x[j], 2) + pow(y[i] - y[j], 2));
86                     cnt++;
87                 }                                
88             }
89         }
90 //        printf("ans = %.2lf\n", ans);
91         kruskal(cnt, ans);
92     }
93     return 0;
94 }

总结:

    刚开始怎么加都不对,总是多加一条本该被另外算的边,然后想要把那条边去掉,想不到好的办法一直WA,突然想到置为0不就好了!还是练太少了,太菜了,加油加油!
    做题,一定要先手算下案例是怎么出来的,是属于哪种情况,这样才好想思路和找对策。

posted @ 2019-07-14 21:48  Anzer  阅读(128)  评论(0编辑  收藏  举报