Eddy's picture

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

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

题目大意:

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

思路:

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

AC代码:(Java)

  1. import java.util.Scanner;
  2. import java.math.*;
  3. public class Main {
  4. public static final int MAXN = 110;
  5. public static double mp[][] = new double[MAXN][MAXN];
  6. public static Point p[] = new Point[MAXN];
  7. public static int n;
  8. public static String answer = new String();
  9. public static Scanner scn = new Scanner(System.in);
  10. public static void main(String[] args){
  11. for (int i = 0; i < MAXN; i++){
  12. p[i] = new Point();
  13. }
  14. while (scn.hasNext()) {
  15. n = scn.nextInt();
  16. foundMap();
  17. answer = String.format("%.2f", prim());
  18. System.out.println(answer);
  19. }
  20. System.exit(0);
  21. }
  22. public static boolean foundMap() {
  23. double x,y;
  24. for (int i = 0; i < n; i++) {
  25. x = scn.nextDouble();
  26. y = scn.nextDouble();
  27. p[i].setPoint(x, y);
  28. }//Found points
  29. for (int i = 0; i < n; i++)
  30. for (int j = 0; j < n; j++)
  31. if (i != j)
  32. mp[i][j] = Point.getDistencs(p[i], p[j]);
  33. //Found map
  34. return true;
  35. }
  36. public static double prim() {
  37. int tempI;
  38. double tempClos;
  39. double[] closEdge = new double[MAXN];
  40. double sum = 0.0;
  41. //Init closeEdge
  42. closEdge[0] = 0.0;
  43. for (int i = 1; i < n; i++) {
  44. closEdge[i] = mp[0][i];
  45. }
  46. //Find n-1 edge
  47. for (int i = 1; i < n; i++) {
  48. tempI = -1;
  49. tempClos = -1.0;
  50. for (int j = 0; j < n; j++) {
  51. if (closEdge[j] != 0.0 && (tempClos == -1.0 || closEdge[j] < tempClos)){
  52. tempClos = closEdge[j];
  53. tempI = j;
  54. }
  55. }
  56. sum += tempClos;
  57. closEdge[tempI] = 0;
  58. for (int j = 0; j < n; j++) {
  59. if (j != tempI && closEdge[j] > mp[tempI][j]) {
  60. closEdge[j] = mp[tempI][j];
  61. }
  62. }
  63. }
  64. return sum;
  65. }
  66. }
  67. class Point { //已下为一个点类 可以不看。
  68. private double x;
  69. private double y;
  70. public Point(double x,double y) {
  71. this.x = x;
  72. this.y = y;
  73. }
  74. public Point() {
  75. x = 0.0;
  76. y = 0.0;
  77. }
  78. public void setPoint(double X,double Y) {
  79. this.x = X;
  80. this.y = Y;
  81. }
  82. public double getX() {
  83. return x;
  84. }
  85. public double getY() {
  86. return y;
  87. }
  88. public static double getDistencs(Point p1,Point p2) {
  89. return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2.0) + Math.pow(p1.getY() - p2.getY(),2.0));
  90. }
  91. }

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. poj 1177 --- Picture(线段树+扫描线 求矩形并的周长)

    题目链接 Description A number of rectangular posters, photographs and other pictures of the same shape a ...

  2. kafka快速开始

    Step 1: Download the code Step 2: Start the server Step 3: Create a topic Step 4: Send some messages ...

  3. IdentityServer Resource Owner Password

    Resource Owner Password 模式需要自己实现用户名密码的校验 新增ResourceOwnerPasswordValidator实现IResourceOwnerPasswordVal ...

  4. 使用ANNdotNET进行情感分析

    2018年10月的MSDN杂志上发表了由James McCaffrey撰写的文章“使用CNTK的情感分析” .在这篇博文中,我将向您介绍这篇非常好且写得很好的MSDN文章示例.我不打算重复MSDN文章 ...

  5. Android--序列化XML数据

    前言 之前有讲过在Android下如何解析XML文件的内容,这篇博客讲讲如何把一个对象序列化为XML格式,有时候一些项目中需要传递一些XML格式的数据.而对于如何解析XML,不了解的朋友可以看看其他三 ...

  6. Windows服务器端口绑定证书

    打开IIS:inetmgr 打开服务:ctrl+shift+Esc 查看证书秘钥: 服务器查看https证书绑定: 1) 查看证书 netsh http show sslcert 2) 将证书与端口绑 ...

  7. 彻底弄懂python编码

    在编写python程序的过程中,中英文混用经常会出现编码问题.围绕此问题,本文首先介绍编码的含义及常用编码,随后列举几个python经常遇到的编码异常及解决方法,接着列举笔者在实践中遇到的异常出现的情 ...

  8. 用初中代数结合python画出正方形

    在屏幕上打印类似下面的图形: 常规画正方形的算法: 这几乎是初学所有计算机语言时都会遇到的问题.算法都大致类似,就是找出打印规律然后用计算机语句表达出来.最常规的算法是:输入数字n就打印n行,首行和尾 ...

  9. samba企业级实战应用详解-技术流ken

    1.简介 Samba是一套使用SMB(Server Message Block)协议的应用程序, 通过支持这个协议, Samba允许Linux服务器与Windows系统之间进行通信,使跨平台的互访成为 ...

  10. __EF批量删除

    控制器: [HttpPost] public int Delete_All(string Id) { List<int> IDS = new List<int>();//创建l ...