hdu1162Eddy's picture

http://acm.hdu.edu.cn/showproblem.php?pid=1162

最小生成树

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<math.h>
 4 #include<string.h>
 5 #include<stdlib.h>
 6 using namespace std;
 7 const int N=5005;
 8 struct stu{
 9     int u;
10     int v;
11     double w;
12 }p[N];
13
14 struct ln{
15     double x;
16     double y;
17 }q[N];
18 int father[N];
19 int n,m,k;
20
21 int cmp(const void *a,const void *b)
22 {
23     return (*(struct stu*)a).w > (*(struct stu*)b).w ?1:-1;
24 }
25 int find(int x)
26 {
27     if(father[x]!=x)
28     father[x]=find(father[x]);
29     return father[x];
30 }
31 int make(int a,int b)
32 {
33     int h=0;
34     int f1=find(a);
35     int f2=find(b);
36     if(f1>f2)
37     {
38         father[f1]=f2;
39         h=1;
40     }
41     else if(f1<f2)
42     {
43         father[f2]=f1;
44         h=1;
45     }
46     return h;
47 }
48 double kruskal()
49 {
50     int cnt=0;
51     double s=0;
52     for(int i=0;i<k;i++)
53     {
54         if(make(p[i].u,p[i].v))
55         {
56             s+=p[i].w;
57             cnt++;
58         }
59         if(cnt==n-1)
60         return s;
61     }
62     return s;
63 }
64 void zuhe()
65 {
66     k=0;
67     for(int i=0;i<n;i++)
68     {
69         for(int j=i+1;j<n;j++)
70         {
71             p[k].u=i+1;
72             p[k].v=j+1;
73             p[k++].w=sqrt((q[i].x-q[j].x)*(q[i].x-q[j].x)+(q[i].y-q[j].y)*(q[i].y-q[j].y));
74         }
75     }
76 }
77
78 int main()
79 {
80     //freopen("in.txt","r",stdin);
81     while(~scanf("%d",&n))
82     {
83         for(int i=1;i<=n;i++)
84         father[i]=i;
85         for(int i=0;i<n;i++)
86         {
87             scanf("%lf%lf",&q[i].x,&q[i].y);
88         }
89         zuhe();
90         qsort(p,k,sizeof(struct stu),cmp);
91 //        for(int i=0;i<k;i++)
92 //        {
93 //            printf("%d %d %lf\n",p[i].u,p[i].v,p[i].w);
94 //        }
95         printf("%.2lf\n",kruskal());
96     }
97     return 0;
98 }

hdu1162Eddy's picture

时间: 2024-08-12 05:37:49

hdu1162Eddy's picture的相关文章

HDU1162-Eddy&#39;s picture(最小生成树)

Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result i

生成树&amp;最短路总结篇

1.模板题  我是用prim搞得 给出每点坐标求最小生成树 hdu1162Eddy's picture 最小生成树 #include <iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int flag1=0; double sum; double arr_list[110][110]; struct Edge { int point; doub

HDU 1828 Picture(矩形周长并)

HDU 1828 Picture 题目链接 题意:给定n个矩形,输出矩形周长并 思路:利用线段树去维护,分别从4个方向扫一次,每次多一段的时候,就查询该段未被覆盖的区间长度,然后周长就加上这个长度,4个方向都加完就是答案 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 5005; int n; struct Rec { int

usaco Picture

/* ID: modengd1 PROG: picture LANG: C++ */ #include <iostream> #include <stdio.h> #include <memory.h> #include <vector> #include <algorithm> using namespace std; int N; struct edge { int y,x1,x2; bool isbegin; bool friend ope

Hole puncher Show Picture

import sys reload(sys) sys.setdefaultencoding('utf8') import matplotlib.pyplot as plt import string file = open("GD.txt") i = 0 x = range(0) y = range(0) for line in file: x.append(string.atoi(line[0:7], 10)) y.append(string.atoi(line[8:], 10))

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b

hdu1828 Picture(线段树+离散化+扫描线)两种方法

C - Picture Time Limit:2000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Description A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or

线性DP POJ2279 Mr.Young&#39;s Picture Permutations

Mr. Young's Picture Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1128   Accepted: 562 Description Mr. Young wishes to take a picture of his class. The students will stand in rows with each row no longer than the row behin

Eddy&#39;s picture(最小生成树)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 228 Accepted Submission(s): 168   Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become