题解报告: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 ...
随机推荐
- python爬虫25 | 爬取下来的数据怎么保存? CSV 了解一下
大家好 我是小帅b 是一个练习时长两年半的练习生 喜欢 唱! 跳! rap! 篮球! 敲代码! 装逼! 不好意思 我又走错片场了 接下来的几篇文章 小帅b将告诉你 如何将你爬取到的数据保存下来 有文本 ...
- python爬虫24 | 搞事情了,用 Appium 爬取你的微信朋友圈。
昨天小帅b看到一些事情不顺眼 有人偷换概念 忍不住就写了一篇反讽 996 的 看不下去了,我支持996,年轻人就该996! 没想到有些人看不懂 这就算了 还来骂我 早些时候关注我的小伙伴应该知道我第一 ...
- C++ 输入外挂
inline int read() { int x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch> ...
- 3.6.5 空串与Null串
空串""是长度为0的字符串.可以调用以下代码检查一个字符串是否为空: String s = "greeting"; ...
- HDU1507 Uncle Tom's Inherited Land*
题目是跟 zoj1516是一样的,但多了匹配后的输出 详解zoj1516可见http://www.cnblogs.com/CSU3901130321/p/4228057.html #include & ...
- FZU 2109 Mountain Number
http://acm.fzu.edu.cn/problem.php?pid=2109 题意:找出区间[l,r]内满足奇数位的数字大于相邻偶数位数字的个数. 典型的数位dp了,记录一下当前位是奇数位还是 ...
- POJ2774:Long Long Message
问两个串的最长公共子串,n<=100000. SAM可以直接搞当然SA哈希都可以..类似于KMP的做法,如果沿parent边走要顺势修改匹配位置. #include<stdio.h> ...
- Win32编程API 基础篇 -- 6.菜单和图标
菜单和按钮 例子:菜单1 本小节仅仅向你展示如果向你的窗口中加入一个基本的菜单,通常你会用到一个提前制作好的菜单资源,这会是一份.rc文件并且会被编译链接进你的.exe可执行程序中.这是具体的流程做法 ...
- 2102 石子归并 2codevs
2102 石子归并 2codevs 题目描述 Description 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为 ...
- Ubuntu安装deb时错误:“dpkg:错误:另外一个进程已经为 dpkg 状态数据库 加锁”解决
以下方式任选一个即可: 1.重启系统 2.执行(这种方式不要尝试,系统很容易挂) sudo rm /var/lib/dpkg/lock 然后执行修复 sudo dpkg --configure -a