Eddy's picture

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162

    ——每天在线,欢迎留言谈论。

题目大意:

给你N个点,求把这N个点连在一起的最短总距离。

思路:

假设每两两点之间都有路径,求最小生成树。

AC代码:(Java)

 import java.util.Scanner;
import java.math.*;
public class Main {
public static final int MAXN = 110;
public static double mp[][] = new double[MAXN][MAXN];
public static Point p[] = new Point[MAXN];
public static int n;
public static String answer = new String();
public static Scanner scn = new Scanner(System.in);
public static void main(String[] args){
for (int i = 0; i < MAXN; i++){
p[i] = new Point();
}
while (scn.hasNext()) {
n = scn.nextInt();
foundMap();
answer = String.format("%.2f", prim());
System.out.println(answer);
}
System.exit(0);
}
public static boolean foundMap() {
double x,y;
for (int i = 0; i < n; i++) {
x = scn.nextDouble();
y = scn.nextDouble();
p[i].setPoint(x, y);
}//Found points
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j)
mp[i][j] = Point.getDistencs(p[i], p[j]);
//Found map
return true;
}
public static double prim() {
int tempI;
double tempClos;
double[] closEdge = new double[MAXN];
double sum = 0.0;
//Init closeEdge
closEdge[0] = 0.0;
for (int i = 1; i < n; i++) {
closEdge[i] = mp[0][i];
}
//Find n-1 edge
for (int i = 1; i < n; i++) {
tempI = -1;
tempClos = -1.0;
for (int j = 0; j < n; j++) {
if (closEdge[j] != 0.0 && (tempClos == -1.0 || closEdge[j] < tempClos)){
tempClos = closEdge[j];
tempI = j;
}
}
sum += tempClos;
closEdge[tempI] = 0;
for (int j = 0; j < n; j++) {
if (j != tempI && closEdge[j] > mp[tempI][j]) {
closEdge[j] = mp[tempI][j];
}
}
}
return sum;
}
}
class Point { //已下为一个点类 可以不看。
private double x;
private double y;
public Point(double x,double y) {
this.x = x;
this.y = y;
}
public Point() {
x = 0.0;
y = 0.0;
}
public void setPoint(double X,double Y) {
this.x = X;
this.y = Y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public static double getDistencs(Point p1,Point p2) {
return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2.0) + Math.pow(p1.getY() - p2.getY(),2.0));
}
}

2017-07-23 13:07:39

HDU 1162 Eddy's picture (最小生成树)(java版)的更多相关文章

  1. hdu 1162 Eddy's picture(最小生成树算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...

  2. hdu 1162 Eddy's picture (最小生成树)

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. 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 ...

  4. HDU 1162 Eddy's picture

    坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...

  5. hdu 1162 Eddy's picture (prim)

    Eddy's pictureTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  6. HDU 1162 Eddy's picture (最小生成树 prim)

    题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...

  7. HDU 1162 Eddy's picture (最小生成树 普里姆 )

    题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...

  8. hdu 1162 Eddy's picture(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...

  9. 题解报告:hdu 1162 Eddy's picture

    Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...

随机推荐

  1. Java线程之 InterruptedException 异常

    Java线程之 InterruptedException 异常   当一个方法后面声明可能会抛出InterruptedException 异常时,说明该方法是可能会花一点时间,但是可以取消的方法. 抛 ...

  2. 使用postman进行并发测试

    1.打开postman软件 左侧栏点击+号键,创建一个并发测试文件夹 2.主面板点击+号键,输入一个测试地址,点击save按钮保存到并发测试文件夹 3.点击三角箭头,再点击Run,弹出Collecti ...

  3. 使用 Mutex 实现进程间同步

    我们知道 Mutex 互斥量是可以用在线程间同步的,线程之间共享进程的数据,mutex 就可以直接引用.而进程有自己独立的内存空间,要怎样将它应用在进程间同步呢?为了达到这一目的,可以在 pthrea ...

  4. 南大算法设计与分析课程复习笔记(2)L2 - Asymptotics

    一.几种比较复杂度的符号 数据结构有描述,相关严格数学定义也不想说了,就这么过了吧. 二.最大子数组的几种解决方法 从最复杂的暴力解法过渡到最简单的动态规划 解析和代码见这里:http://www.c ...

  5. 第一册:lesson twenty five。

    原文:Mrs.smith's kitchen. Mrs.smith's kitchen is small. There is a refrigerator in the kitchen. The re ...

  6. EntityFramework(1)基础概念与Database First

    基本概念 EntityFramework是微软推出的一款ORM工具,关于ORM的概念请参考博客https://www.cnblogs.com/huanhang/p/6054908.html.这篇文章对 ...

  7. @Html.xxxxxFor() 规范写法

    @Html.TextBoxFor() 讲解(其他类似的 @Html.LabelFor 等)同理 @Html.TextBoxFor(model => model.SearchParams.Name ...

  8. JQuery官方学习资料(译):$( document ).ready()

         一个页面直到document是”ready“才能被安全的操作,Jquery为你检查这种状态.代码包含在$( document ).ready()的内部将会仅仅运行一次在页面Document ...

  9. Xhprof graphviz Warning: proc_open() [function.proc-open]: CreateProcess failed, error code 解决方法

    Xhprof在windows下点击[View Full Callgraph]调用graphviz软件时.警告Warning: proc_open() [function.proc-open]: Cre ...

  10. JVM运行时数据区内容简述

    JVM运行时数据区分为五个部分:程序计数器.虚拟机栈.本地方法栈.堆.方法区.如下图所示,五部分其中又分为线程共享区域和线程私有区域,下面将分别介绍每一部分. 1. PC程序计数器 程序计数器是一块较 ...