標籤:sort 檔案的 整數 next 測試資料 font style sys 表串連
目錄
1 問題描述
2 解決方案
1 問題描述問題描述給定帶權無向圖,求出一顆方差最小的產生樹。輸入格式輸入多組測試資料。第一行為N,M,依次是點數和邊數。接下來M行,每行三個整數U,V,W,代表串連U,V的邊,和權值W。保證圖連通。n=m=0標誌著測試檔案的結束。輸出格式對於每組資料,輸出最小方差,四捨五入到0.01。輸出格式按照範例。範例輸入4 5
1 2 1
2 3 2
3 4 2
4 1 1
2 4 3
4 6
1 2 1
2 3 2
3 4 3
4 1 1
2 4 3
1 3 3
0 0範例輸出Case 1: 0.22
Case 2: 0.00資料規模與約定
1<=U,V<=N<=50,N-1<=M<=1000,0<=W<=50。資料不超過5組。
2 解決方案
本題主要考查Kruskal演算法,其中的重點在於並查演算法的應用,在尋找最小平方差的最小產生樹時,需要枚舉邊權值的均值。
但是,依照這樣的方法,在藍橋練習系統中測評一直為50分,在網上找了一下其他網友寫的C代碼,提交也是50分,可能是藍橋練習系統的後台測試資料有點問題,也有可能是本題枚舉的精確度不夠。
具體代碼如下:
import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Scanner;public class Main { public static int n, m; public static double minV; //輸入所有邊中權值最小的邊 public static double maxV; //輸入所有邊中權值最大的邊 public static int[] id; public static ArrayList<edge> map; public static ArrayList<Double> result = new ArrayList<Double>(); class MyComparator implements Comparator<edge> { public int compare(edge arg0, edge arg1) { if(arg0.w > arg1.w) return 1; else if(arg0.w < arg1.w) return -1; return 0; } } static class edge { public int a; //邊的起點 public int b; //邊的終點 public double v; //邊的權值 public double w; //邊權的方差值 public edge(int a, int b, double v) { this.a = a; this.b = b; this.v = v; this.w = 0; } } public void init() { minV = Double.MAX_VALUE; maxV = Double.MIN_VALUE; map = new ArrayList<edge>(); } public int find(int a) { int root = a; while(id[root] >= 0) { root = id[root]; } int k = a, i; while(k != root) { i = id[k]; id[k] = root; k = i; } return root; } public void union(int a, int b) { int rootA = find(a); int rootB = find(b); if(rootA == rootB) return; int num = id[rootA] + id[rootB]; if(id[rootA] < id[rootB]) { id[rootB] = rootA; id[rootA] = num; } else { id[rootA] = rootB; id[rootB] = num; } } public void getResult() { double avg = minV; double minResult = Double.MAX_VALUE; for(;avg <= maxV;avg = avg + 0.3) { //此處是解決本題的關鍵,即枚舉最小產生樹的邊權的均值 for(int i = 0;i < map.size();i++) { double v = map.get(i).v - avg; map.get(i).w = v * v; } Collections.sort(map, new MyComparator()); id = new int[n + 1]; for(int i = 1;i <= n;i++) id[i] = -1; double sum = 0; double[] value = new double[n - 1]; int count = 0; for(int i = 0;i < map.size();i++) { int rootA = find(map.get(i).a); int rootB = find(map.get(i).b); if(rootA != rootB) { union(map.get(i).a, map.get(i).b); value[count++] = map.get(i).v; sum += map.get(i).v; if(count == n - 1) break; } } sum = sum / (n - 1); double temp = 0; for(int i = 0;i < value.length;i++) { temp = temp + (value[i] - sum) * (value[i] - sum); } temp = temp / (n - 1); if(minResult > temp) minResult = temp; } result.add(minResult); } public static void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(System.in); while(true) { n = in.nextInt(); m = in.nextInt(); if(n == 0 || m == 0) break; test.init(); for(int i = 1;i <= m;i++) { int a = in.nextInt(); int b = in.nextInt(); double v = in.nextDouble(); map.add(new edge(a, b, v)); minV = Math.min(minV, v); maxV = Math.max(maxV, v); } test.getResult(); } for(int i = 0;i < result.size();i++) { System.out.print("Case "+(i+1)+": "); System.out.printf("%.2f", result.get(i)); System.out.println(); } }}
演算法筆記_164:演算法提高 最小方差產生樹(Java)