题解报告:hdu 1162 Eddy's picture
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?
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int n;
bool vis[maxn];
double lowdist[maxn],dist[maxn][maxn];
pair<double,double> point[maxn];
double vecx(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double Prim(){
for(int i=;i<=n;++i)
lowdist[i]=dist[][i];
lowdist[]=;vis[]=true;
double res=0.0;
for(int i=;i<n;++i){
int k=-;
for(int j=;j<=n;++j)
if(!vis[j] && (k==-||lowdist[k]>lowdist[j]))k=j;
if(k==-)break;
vis[k]=true;
res+=lowdist[k];
for(int j=;j<=n;++j)
if(!vis[j])lowdist[j]=min(lowdist[j],dist[k][j]);
}
return res;
}
int main()
{
while(cin>>n){
for(int i=;i<=n;++i)
cin>>point[i].first>>point[i].second;
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
dist[i][j]=vecx(point[j].first,point[j].second,point[i].first,point[i].second);
memset(vis,false,sizeof(vis));
cout<<setiosflags(ios::fixed)<<setprecision()<<Prim()<<endl;
}
return ;
}
AC代码之Kruskal算法:
#include<bits/stdc++.h>
using namespace std;
const int maxn = ;
const int maxc = ;//100*100+5
int n,k,father[maxn];
double lowdist;
pair<double,double> point[maxn];//点的坐标
struct edge{int u,v;double dist;}es[maxc];
bool cmp(const edge& e1,const edge& e2){return e1.dist<e2.dist;}
double vecx(double x1,double y1,double x2,double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
int find_father(int x){//找根节点
int pir=x,tmp;
while(father[pir]!=pir)pir=father[pir];
while(x!=pir){
tmp=father[x];
father[x]=pir;//路径压缩
x=tmp;
}
return x;
}
void unite_father(int x,int y,double z){
x=find_father(x);
y=find_father(y);
if(x!=y){
lowdist+=z;
father[x]=y;
}
}
void Kruskal(){
for(int i=;i<=n;++i)father[i]=i;
lowdist=0.0;
sort(es,es+k,cmp);
for(int i=;i<k;++i)
unite_father(es[i].u,es[i].v,es[i].dist);
}
int main()
{
while(cin>>n){
for(int i=;i<=n;++i)
cin>>point[i].first>>point[i].second;
k=;
for(int i=;i<=n;++i){
for(int j=;j<=n;++j){
es[k].u=i;es[k].v=j;
es[k++].dist=vecx(point[j].first,point[j].second,point[i].first,point[i].second);
}
}
Kruskal();
cout<<setiosflags(ios::fixed)<<setprecision()<<lowdist<<endl;
}
return ;
}
题解报告:hdu 1162 Eddy's picture的更多相关文章
- hdu 1162 Eddy's picture (Kruskal 算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...
- hdu 1162 Eddy's picture(最小生成树算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1162 Eddy's picture (最小生成树)(java版)
Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 ——每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...
- HDU 1162 Eddy's picture
坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
- hdu 1162 Eddy's picture (最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1162 Eddy's picture (prim)
Eddy's pictureTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1162 Eddy's picture (最小生成树 prim)
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- HDU 1162 Eddy's picture (最小生成树 普里姆 )
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- hdu 1162 Eddy's picture(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...
随机推荐
- nginx 4 win10
去下载文件 http://nginx.org/en/download.html 然后释放文件到一目录 最后执行nginx.exe.到浏览器查看localhost,界面: 在最后,别忘了,修改其80端口 ...
- java使用JNA框架调用dll动态库
这两天了解了一下java调用dll动态库的方法,总的有三种:JNI.JNA.JNative.其中JNA调用DLL是最方便的. ·JNI ·JNA ·JNative java使用 JNI来调用dll动态 ...
- C#装饰模式
using System;using System.Collections.Generic;using System.Text; namespace 装饰模式{ class Person ...
- 【BZOJ1014】火星人prefix(splay,Hash)
题意: . 思路: ; ..,..]of longint; sum,size,fa,a,b,id,mi:..]of longint; n,m,i,x,y,s,k,j,cnt,root:longint; ...
- 【Eclipse】Eclipse 快捷键
Eclipse 快捷键 关于快捷键 Eclipse 的很多操作都提供了快捷键功能,我们可以通过键盘就能很好的控制 Eclipse 各个功能: 使用快捷键关联菜单或菜单项 使用快捷键关联对话窗口或视图或 ...
- codevs 1296 营业额统计 (splay 点操作)
题目大意 每次加入一个值,并且询问之前加入的数中与该数相差最小的值. 答案输出所有相差值的总和. 解题分析 = = 参考程序 #include <bits/stdc++.h> using ...
- centos7 mysql安装与用户设置
1.环境:Centos 7.0 64位2.mysql版本:5.73.安装:https://dev.mysql.com/doc/refman/5.7/en/installing.html3.1.创建my ...
- [bzoj1821][JSOI2010]部落划分(贪心)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1821 分析:题目看起来很吊,但只要贪心就行了,每次取相邻最近的两个点所在的集合合并知道 ...
- samba 奇怪问题
有一个centos 7 samba服务器,配置如下: [root@proxy223 20150331]# cat /etc/samba/smb.conf [global] workgroup = W ...
- A Complete Guide to Usage of ‘usermod’ command– 15 Practical Examples with Screenshots
https://www.tecmint.com/usermod-command-examples/ -------------------------------------------------- ...