题解报告: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 ...
随机推荐
- PAT 1141 PAT Ranking of Institutions
After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...
- Git——跟踪或取消跟踪文件
在Git是用过程中,可能遇到以下情况: 1.被跟踪文件里面有不想跟踪的文件. 2.每次用git status查看状态时总是列出未被跟踪的文件. 解决方法: 1.当被跟踪的文件里面有不想跟踪的文件时,使 ...
- getContextPath和getRealPath的区别-----其实主要区别就是相对路径和绝对路径
getContextPath和getRealPath的区别 其实主要区别就是相对路径和绝对路径 https://blog.csdn.net/zsmj_2011/article/details/4121 ...
- POJ——T3417 Network
http://poj.org/problem?id=3417 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5294 A ...
- 【APIO2012】【BZOJ2809】派遣dispatching
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MB Submit: 1932 Solved: 967 [Submit ...
- curl -O 下载文件
curl -O 下载文件 学习了:http://blog.csdn.net/wulong710/article/details/53127606 curl -O http://a.b.c/a.tar ...
- Android 好看的搜索界面,大赞Animation
转载请注明出处王亟亟的大牛之路 一直对Animation属于可有可无的不在意.看到个样例,认为在不切换的情况下,适当的应用还真是蛮好看的. 包结构: 一个类一个控件.内容简单. 执行效果: 下方的下方 ...
- 数据库数据在Java占用内存简单估算
数据库数据在Java占用内存简单估算 结论: 1.数据库记录放在JAVA里,用对象(ORM一般的处理方式)须要4倍左右的内存空间.用HashMap这样的KV保存须要10倍空间; 2.假设你主要数据是t ...
- HDFS01
==============NameNode============== 管理文件系统的命名空间 记录每个文件数据在各个DataNode上的位置和副本信息 协调客户端对文件的访问 NameNode文件 ...
- mysql20170410练习代码+笔记
今天的几道高级sql查询真的挺难的,感觉好像视频里讲过,当时也没有练,已经淡化了很多,sql还是要多练习啊!确实逻辑性挺强的. SELECT studentResult,studentNO FROM ...