首页 > 代码库 > HDU 3405
HDU 3405
World Islands
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1493 Accepted Submission(s): 594
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
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
billionaires
想买一座岛,但他不想让自己的岛与其他岛相连,所以在剩下的n-1座岛屿之间架桥,最短路径是多少PS:自己刚开使只用了一个Kruskal wa 必须遍历
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <queue> #include <cstdlib> #include <iomanip> #include <cmath> #include <ctime> #include <map> #include <set> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define MAX 100000000000000000 #define MOD 1000000007 #define pi acos(-1.0) #define ei exp(1) #define PI 3.141592653589793238462 #define ios() ios::sync_with_stdio(false) #define INF 0x3f3f3f3f #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int f[70]; int n,m,t; struct Node { double x; double y; }node[55]; struct E { int u; int v; double w; friend bool operator<(const E &a,const E&b) { return a.w<b.w; } }e[3000]; double dist(Node a,Node b) { return (double)sqrt(((a.x-b.x)*(a.x-b.x)*1.0)+((a.y-b.y)*(a.y-b.y)*1.0)); } int find(int x) { return f[x]==x?x:find(f[x]); } double kruskal() { double ans=0.0; int k=1,q=n-1; for(int i=0;i<=n;i++) { f[i]=i; } for(int i=0;i<m;i++) { int u=find(e[i].u); int v=find(e[i].v); if(u!=v) { ans+=e[i].w; if(u<v) f[u]=v; else f[v]=u; k++; if(k==q) break; } } return ans; } int main() { scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%lf %lf",&node[i].x,&node[i].y); } double ans=1000000.0; for(int k=0;k<n;k++) { m=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(i==k || j==k) continue; e[m].u=i; e[m].v=j; e[m++].w=dist(node[i],node[j]); } } sort(e,e+m); ans=min(ans,kruskal()); } printf("%.2f\n",ans); } return 0; }
HDU 3405
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。