4002
来源:互联网 发布:dns的协议端口号 编辑:程序博客网 时间:2024/05/16 05:22
4002
Problem B
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 58 Accepted Submission(s) : 18
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.<br>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?<br>
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. <br><br>Input contains multiple test cases. Process to the end of file.<br>
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. <br>
Sample Input
31.0 1.02.0 2.02.0 4.0
Sample Output
3.41
题意:用最短的连线把所有的点连起来;
思路:大方向很好想,最小生成树+并查集;
但是代码实现很困难。
在网上找到了别人的代码。参考着写的。
初始化的时候把边初始化为了0,出了很多问题;
AC代码:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = 105;
struct Point {
double x,y;
}p[N];
struct Edge{
int s,e;
double len;
}edge[N*(N-1)/2];
int father[N],n;
double dis(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
double kruskal(int m){
sort(edge+1,edge+m+1,cmp);
double cost=0;
for(int i=1;i<=m;i++){
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
cost +=edge[i].len;
}
}
return cost;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++) father[i] = i;
for(int i=0;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
int m =1; ///边的数量
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){ ///这里是i+1开始,从0开始会多出很多边
edge[m].s =i;
edge[m].e = j;
edge[m++].len = dis(p[i],p[j]);
}
}
m--; ///记得
printf("%.2lf\n",kruskal(m));
}
}
思路:大方向很好想,最小生成树+并查集;
但是代码实现很困难。
在网上找到了别人的代码。参考着写的。
初始化的时候把边初始化为了0,出了很多问题;
AC代码:
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N = 105;
struct Point {
double x,y;
}p[N];
struct Edge{
int s,e;
double len;
}edge[N*(N-1)/2];
int father[N],n;
double dis(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
double kruskal(int m){
sort(edge+1,edge+m+1,cmp);
double cost=0;
for(int i=1;i<=m;i++){
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
cost +=edge[i].len;
}
}
return cost;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++) father[i] = i;
for(int i=0;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
int m =1; ///边的数量
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){ ///这里是i+1开始,从0开始会多出很多边
edge[m].s =i;
edge[m].e = j;
edge[m++].len = dis(p[i],p[j]);
}
}
m--; ///记得
printf("%.2lf\n",kruskal(m));
}
}
0 0
- 4002
- hdu 4002
- HDU 4002
- Android学习笔记4002
- 【hadoop】 4002-Combiner组件
- 4002 大数 java
- 【4002】括号序列
- HDU 4002 数论
- hdoj 4002Find the maximum
- hdu 4002 - Find the maximum
- HDU 4002 欧拉函数
- BZOJ 4002~4007 JLOI2015 代码
- 4002: [JLOI2015]有意义的字符串
- 4002:谁是你的潜在朋友
- POJ-4002:谁是潜在的朋友
- poj 4002:谁是你的潜在朋友
- poj 4002 Alice's mooncake shop
- bzoj 4002 [JLOI2015]有意义的字符串 数学
- 备忘录模式(Memento)
- leetcode 169 Majority Element
- mysql 存储过程学习
- UI调试工具 Reveal
- 【HDOJ】【百度之星】5700 区间交
- 4002
- 了解使用Android ConstraintLayout
- 扑克牌游戏
- JavaMail
- hdu 5400 Arithmetic Sequence(重构数组)
- c++中的const
- SQL 存储过程回滚
- Problem G: C语言习题 n个数逆序
- 树莓派的关机命令