HDU 1162Eddy's picture(MST问题)
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11841 Accepted Submission(s): 5922
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 it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?
Input
The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.
Input contains multiple test cases. Process to the end of file.
Output
Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.
Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
Sample Output
3.41
Author
eddy
Recommend
JGShining | We have carefully selected several similar problems for you: 1217 1875 1879 1863 1325
分析:
最小生成树问题(采用克鲁斯卡尔算法)
不过边还需要自己求(两点间的距离公式,点的组合)
code:
#include<bits/stdc++.h>
using namespace std;
#define max_v 105
struct edge
{
int x,y;
double w;
};
edge e[max_v*max_v];
int pa[max_v],rk[max_v];
double sum=0.0;
bool cmp(edge a,edge b)
{
return a.w<b.w;
}
void make_set(int x)
{
pa[x]=x;
rk[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y,double w)
{
x=find_set(x);
y=find_set(y);
if(x==y)
return ;
if(rk[x]>rk[y])
pa[y]=x;
else
{
if(rk[x]==rk[y])
rk[y]++;
pa[x]=y;
}
sum+=w;
return ;
}
double f(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int main()
{
int n;
while(~scanf("%d",&n))
{
sum=;
double a[max_v],b[max_v];
for(int i=; i<n; i++)
{
make_set(i);
scanf("%lf %lf",&a[i],&b[i]);
}
int k=;
for(int i=; i<n; i++)
{
for(int j=i+; j<n; j++)
{
e[k].x=i;
e[k].y=j;
e[k++].w=f(a[i],b[i],a[j],b[j]);
}
}
sort(e,e+k,cmp);
for(int i=; i<k; i++)
{
union_set(e[i].x,e[i].y,e[i].w);
}
printf("%0.2lf\n",sum);
}
return ;
}
HDU 1162Eddy's picture(MST问题)的更多相关文章
- 【HDU 1828】 Picture (矩阵周长并,线段树,扫描法)
[题目] Picture Problem Description A number of rectangular posters, photographs and other pictures of ...
- hdu Eddy's picture (最小生成树)
Eddy's picture Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- HDU 5627 Clarke and MST &意义下最大生成树 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5627 题意:Bestcoder的一道题,让你求&意义下的最大生成树. 解法: 贪心,我们从高位 ...
- hdu 1679 The Unique MST (克鲁斯卡尔)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24152 Accepted: 8587 D ...
- 【49.23%】【hdu 1828】Picture
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- hdu 1863 - 畅通工程(MST)
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 5627 Clarke and MST(最大 生成树)
Problem Description Clarke is a patient with multiple personality disorder. One day he turned into a ...
- HDU - 4786 Fibonacci Tree (MST)
题意:给一张由白边和黑边构成的无向图,求是否存在一个生成树,使白边的数量为一个斐波那契数. 分析:白边权值为1,黑边权值为0.求出该图的最小生成树和最大生成树,若这两个值之间存在斐波那契数,则可以,若 ...
- HDU 1828:Picture(扫描线+线段树 矩形周长并)
题目链接 题意 给出n个矩形,求周长并. 思路 学了区间并,比较容易想到周长并. 我是对x方向和y方向分别做两次扫描线.应该记录一个pre变量,记录上一次扫描的时候的长度,对于每次遇到扫描线统计答案的 ...
随机推荐
- json格式对象大括号中不能把键改为变量问题
今天遇到了一个往json中写入变量的问题,下面代码是错误的写法 document.querySelector(".box").onclick = function(){ // 移动 ...
- LOJ6066:「2017 山东一轮集训 Day3」第二题
传送门 二分答案 \(k\),考虑如何 \(hash\) 使得做起来方便 把每个点挂在 \(k+1\) 级祖先上,考虑在祖先上删除 这道题巧妙在于其可以对于 \(dfs\) 序/括号序列 \(hash ...
- 原型链中的prototype、__proto__和constructor的关系
先来看一张图,这张图可以说是围绕以下代码完整的描述了各对象之间的关系.接下来我们来看看如何一步步画出这张图. function Foo(){}; var foo = new Foo(); 首先,明确几 ...
- 收藏的几个关于php面向对象教程
面向对象的分析方法是利用面向对象的信息建模概念,如实体.关系.属性等,同时运用封装.继承.多态等机制来构造模拟现实系统的方法,学会了面向对象思想,能够大大提高php编程开发效率!本篇文章php中文网将 ...
- drupal 通过hook_menu实现添加菜单
$items['mypayment/onlinepay']=array( 'title' => '在线充值', 'description' => '在线充值', 'page callbac ...
- Vue.js学习(常用指令)
Vue.js的指令是以v-开头,它们用于HTML元素,指令提供了一些特殊的特性,将指令绑定在元素上时,指令会为绑定的目标元素添加一些特殊的行为,我们可以将指令看作特殊的HTML特性. 本文参考:htt ...
- js获取日期:昨天今天和明天、后天 [转贴记录]
<html> <head> <meta http-equiv="Content-Type" content="textml; charset ...
- kali 的端口扫描nmap
输入“nmap+空格+“-O”+空格+IP地址或域名. 扫描所有TCP端口:输入“nmap+空格+“-sT”+空格+IP地址或域名” 扫描所有开放的UDP端口:输入“nmap+空格+”-sP”+空格+ ...
- Akka - Basis for Distributed Computing
Some concepts as blow: Welcome to Akka, a set of open-source libraries for designing scalable, resil ...
- toLocaleTimeString()方法在IE和谷歌浏览器上 根据本地时间格式,把 Date 对象的时间部分(不含日期)转换为“时间字符串”存在区别
这两天修改一个bug,发现一个问题: toLocaleTimeString()方法在IE和谷歌浏览器上 根据本地时间格式,把 Date 对象的时间部分(不含日期)转换为“时间字符串”存在区别.方法原 ...