题解报告: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 ...
随机推荐
- 创建sum求多元素的和
a = [1, 2, 3] b = [4, 5, 6] def sum_super(* args): s = 0 for i in args: s += sum(i) return s # print ...
- 泛型转换https://www.cnblogs.com/eason-chan/p/3633210.html
import java.lang.reflect.ParameterizedType;import java.lang.reflect.Type;//总结1.st.getClass==Student. ...
- Scala解析Json格式
Scala解析Json格式 代码块 Scala原生包 导入包 import scala.util.parsing.json._ def main(args: Array[String]): Unit ...
- CF576D. Flights for Regular Customers
n<=150个点,m<=150条路,每条路Ai,Bi,Di表示Ai到Bi有一条有向边,使用他前至少要走Di条路,问1到n最少走几条路. 又是n^4过150的题.... 不同于传统的最短路, ...
- P1656 炸铁路 洛谷
https://www.luogu.org/problem/show?pid=1656 题目描述 因为某国被某红色政权残酷的高压暴力统治.美国派出将军uim,对该国进行战略性措施,以解救涂炭的生灵. ...
- Redis是单线程的
Redis是单线程的 学习了: http://blog.csdn.net/liupeng_qwert/article/details/77263187 https://www.cnblogs.com/ ...
- putty SSH出现乱码
解决方法如下: 打开PuTTY主程序 选择window-〉Appearance-〉Font settings-〉点击Change.按钮,字体中选择"新宋体". 选择window-〉 ...
- javaScript 超时与间歇掉用
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 局域网部署docker--从无到有创建自己的docker私有仓库
因为GFW的关系.国内用户在使用docker的时候,pull一个主要的镜像都拉下来.更不用说使用官方的index镜像了.差点放弃使用docker了,google了一圈.总算找到办法. 第一步:安装do ...
- View载入具体解释
文章一開始我要对前面一篇文章做点补充 相信大家都知道View有两个方法. public boolean post(Runnable action) public boolean postDelayed ...