HDU 3405 World Islands

World Islands

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 3405
64-bit integer IO format: %I64d      Java class name: Main
 
Dubai is a haven for the rich. The government of Dubai finds a good way to make money. They built a lot of artificial islands on the sea and sell them. These islands are shaped into the continents of the world, so they are called “world islands”. All islands are booked out now. The billionaires who buy these islands wants to make friends with each other, so they want these islands all be connected by bridges. Bill Gates also has booked an island, but he is the only one who doesn’t want his island to be connected with other islands, because he prefer to travel on the sea on his old landing craft which is used in the Invasion of Normandy in World War II. Fortunately, Bill doesn’t care about which island is saved for him, so Dubai government can still find the best way to build the bridges. The best way means that the total length of the bridges is minimum. In a word, if there are n islands, what they should do is to build n–2 bridges connecting n-1 islands, and give the rest island to Bill Gates. They can give any island to Bill Gates. Now they pay you good money to help them to find out the best way to build the bridges. 
Please note;
1.An island can be considered as a point.
2.A bridge can be considered as a line segment connecting two islands.
3.A bridge connects with other bridges only at the islands.
 

Input

The first line is an integer indicating the number of test cases.
For each test case, the first line is an integer n representing the number of islands.(0<n<50)
Then n lines follow. Each line contains two integers x and y( -20 <= x, y <= 20 ) , indicating the coordinate of an island.
 

Output

For each test case, output the minimum total length of the bridges in a line. The results should be rounded to 2 digits after decimal point, and you must keep 2 digits after the decimal point.
 

Sample Input

2
5
0 0
1 0
18 0
0 1
1 1
3
0 0
1 0
0 1

Sample Output

3.00
1.00

Source

 
解题:每次去掉一个点。。。进行枚举。。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 using namespace std;
 6 const int maxn = 55;
 7 const int INF = 0x3f3f3f3f;
 8 struct Point {
 9     int x,y;
10 } p[maxn];
11 double dis[maxn][maxn],tmp[maxn][maxn],d[maxn];
12 bool done[maxn];
13 int n;
14 double prim(){
15     double ans = 0;
16     for(int i = 0; i < n; ++i){
17         done[i] = false;
18         d[i] = INF;
19     }
20     d[0] = 0;
21     for(int k = 0; k < n; ++k){
22         double minV = INF;
23         int idx = 0;
24         for(int i = 0; i < n; ++i)
25             if(!done[i] && minV > d[i]) minV = d[idx = i];
26         ans += d[idx];
27         done[idx] = true;
28         for(int i = 0; i < n; ++i)
29             if(!done[i] && d[i] > dis[idx][i])
30                 d[i] = dis[idx][i];
31     }
32     return ans;
33 }
34 double getDis(int i,int j){
35     double tmp = (p[i].x - p[j].x)*(p[i].x - p[j].x);
36     tmp += (p[i].y - p[j].y)*(p[i].y - p[j].y);
37     return sqrt(tmp);
38 }
39 int main(){
40     int T;
41     scanf("%d",&T);
42     while(T--){
43         scanf("%d",&n);
44         for(int i = 0; i < n; ++i){
45             scanf("%d %d",&p[i].x,&p[i].y);
46             for(int j = i - 1; j >= 0; --j){
47                 tmp[i][j] = tmp[j][i] = getDis(i,j);
48             }
49         }
50         double ans = INF;
51         for(int i = 0; i < n; ++i){
52             memcpy(dis,tmp,sizeof(tmp));
53             for(int j = 0; j < n; ++j)
54                 dis[i][j] = dis[j][i] = INF>>1;
55             ans = min(ans,prim());
56         }
57         printf("%.2f\n",ans-(INF>>1));
58     }
59     return 0;
60 }
View Code

 

posted @ 2015-04-07 15:59  狂徒归来  阅读(239)  评论(0编辑  收藏  举报