hdu 3405 World Islands(最小生成树)
来源:互联网 发布:菜单制作软件 编辑:程序博客网 时间:2024/05/29 03:33
Problem 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.
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.
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
250 01 018 00 11 130 01 00 1
Sample Output
3.001.00
题意:给你n个点的坐标,问你选出n-1个点使它们连通,并保证边的总长度最小。
枚举最小生成树
#include <iostream>#include <stdio.h>#include <math.h>#include <stdlib.h>using namespace std;const int MAX=55;const double maxn=100000000.0;double map[MAX][MAX];double prime(int u0,int n){ bool vis[MAX]={false}; double Lowcost[MAX]; double sum=0; vis[u0]=true; for (int i=0; i<n; i++) { Lowcost[i]=map[i][0]; } vis[0]=true; if(u0==0) { for (int i=0; i<n; i++) { Lowcost[i]=map[i][1]; } vis[1]=0; } for (int i=0; i<n-1; i++) { int k=-1; double Min=maxn; for (int j=0; j<n; j++) { if(!vis[j] && Min>Lowcost[j]) { Min=Lowcost[j]; k=j; } } if(k==-1) { break; } sum+=Min; vis[k]=true; for (int j=0; j<n; j++) { if(!vis[j] && Lowcost[j]>map[k][j]) { Lowcost[j]=map[k][j]; } } } return sum;}int main(){ int t,n; int x[MAX]; int y[MAX]; scanf("%d",&t); while (t--) { scanf("%d",&n); for (int i=0; i<n; i++) { scanf("%d%d",&x[i],&y[i]); } for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { map[i][j]=maxn; } } for (int i=0; i<n; i++) { for (int j=i+1; j<n; j++) { map[i][j]=map[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])); } } double ans=maxn; for (int i=0; i<n; i++) { ans=min(ans,prime(i,n)); } printf("%.2lf\n",ans); }}
0 0
- hdu 3405 World Islands(最小生成树)
- HDU 3405 World Islands(最小生成树)
- hdu 3405 world islands
- hdu 3405 World Islands
- World Islands HDU3405 杭州省赛 最小生成树
- POJ 3771 World Islands 最小生成树+删点
- HDU 3405 World Islands(最短路)
- HDU 3405 World Islands (prim算法)
- hdu 2122 Ice_cream’s world III(最小生成树)
- hdu 2122 Ice_cream’s world III(最小生成树)
- Ice_cream’s world III hdu 2122 最小生成树
- HDU 2122 Ice_cream’s world III 【最小生成树】
- hdu 2122 Ice_cream’s world III 最小生成树prim
- hdu 2122 Ice_cream’s world III(最小生成树))
- hdu 2122(Ice_cream’s world III)(最小生成树,两种算法都可以)
- HDU 2122 Ice_cream’s world III(最小生成树Kruskal)
- 文章标题 HDU 2122: Ice_cream’s world III (最小生成树+kruskal)
- 最小生成树 Prim 算法HDU 2122 Ice_cream’s world III
- wireshark 转端口显示
- java io详解
- 如何将VC做的应用程序改为一个服务程序
- 第八周 项目二 工资问题
- Dede小企业网站开发流程及常用标签(一)
- hdu 3405 World Islands(最小生成树)
- poj 1330
- 模拟IC芯片设计开发的流程
- 酌坐追锥注最滓租鬃状啄卒壮邹着邹揍酌
- C++ 60分钟入门教程 - 3、C++名字空间
- 籽资赚坠拽尊坐鬃总滓坐左赚赘足撰椎砖
- 《数据结构》第二章线性表学习总结
- 使用 /sys 文件系统访问 Linux 内核
- VC关于windows服务状态更改