注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

东月之神

在单纯的观念里面,生命就容易变得比较深刻!

 
 
 

日志

 
 
关于我

别驻足,梦想要不停追逐,别认输,熬过黑暗才有日出,要记住,成功就在下一步,路很苦,汗水是最美的书!

网易考拉推荐

zstu World Islands  

2010-05-14 10:32:43|  分类: ACM |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Description

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
该题就是去掉任意一个点,然后求最小生成树。求出最小的那个就可以了。有点懒,直接用了模板。。很不好。。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
struct Point
{
 double x, y;
};
inline double dis(Point a, Point b)
{
 return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}
#define MAXN 51
#define inf 1000000000
double min1[MAXN];
double prim(int n, double mat[][MAXN], int* pre)
{
 double ret=0;
 int v[MAXN],i,j,k;
 for (i=0;i<n;i++)
  min1[i]=inf,v[i]=0,pre[i]=-1;
 for (min1[j=0]=0;j<n;j++)
 {
  for (k=-1,i=0;i<n;i++)
   if (!v[i]&&(k==-1||min1[i]<min1[k]))
    k=i;
  for (v[k]=1,ret+=min1[k],i=0;i<n;i++)
   if (!v[i]&&mat[k][i]<min1[i])
    min1[i]=mat[pre[i]=k][i];
 }
 return ret;
}
double s[51][51], a[51][51];
int p1[51], p2[51];
int main()
{
 int t, n, i, j, k;
 while(scanf("%d", &t) != EOF)
 {
  while(t--)
  {
   Point p[51];
   scanf("%d", &n);
   for(i = 0; i < n; i++)
   {
    scanf("%lf%lf", &p[i].x, &p[i].y);
    p1[i] = 0;
    p2[i] = 0;
   }
   if(n <= 2)
   {
    printf("0.00\n");
    continue;
   }
   double ans = 10*inf, cnt = 0;
   for(i = 0; i < n; i++)
    for(j = 0; j < n; j++)
     s[i][j] = inf;
   for(i = 0; i < n; i++)
    for(j = 0; j < n; j++)    
     s[i][j] = dis(p[i], p[j]);
   for(i = 0; i < n; i++)
   {
    for(j = 0; j < n; j++)
     for(k = 0; k < n; k++)
      a[j][k] = s[j][k];
    for(j = 0; j < n; j++)
     a[j][i] = a[i][j] = inf;
    cnt = prim(n, a, p1);
    if(cnt < ans)
     ans = cnt;
   }   
   printf("%.2lf\n", ans-inf);
  }
 }
return 0;
}

  评论这张
 
阅读(105)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017