HDU 1162 Eddy's picture (最小產生樹)(java版)

來源:互聯網
上載者:User

標籤:scn   show   can   while   set   線上   for   href   pow   

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 points29         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 map34         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 closeEdge42         closEdge[0] = 0.0;43         for (int i = 1; i < n; i++) {44             closEdge[i] = mp[0][i];45         }46         //Find n-1 edge47         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版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.