###前提・実現したいこと
全頂点を巡る最小全域木
全域木の最小の重みを求めるアルゴリズムらしいですが、
nextMin()の使われ方、reach[]の変移などがわかりにくいのですが、日本語に意訳出来る方いませんか?
入力
5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1
-1は点と点が接続していない
leng[x][y]は、x からy への辺の重み
全頂点をめぐる回路・全域木の最小の経路・全域木をもとめる
###発生している問題・エラーメッセージ
この辺りの処理の流れがわかりません
for (int i = 2; i < reach.length; i++) nextMin(); private void nextMin() { int min = Integer.MAX_VALUE; int xx = 0, yy = 0; for (int x = 0; x < leng.length; x++) { if (reach[x] == -1) continue; for (int y = 0; y < leng[x].length; y++) { if (reach[y] != -1||leng[x][y]==-1) continue; if (leng[x][y] < min) { xx = x; yy = y; min = leng[x][y]; } } } reach[yy] = min; }
###該当のソースコード
Java
1import java.util.Scanner; 2 3public class Main { 4 5 public static void main(String[] args){ 6 7 Scanner scan = new Scanner(System.in); 8 9 int n = scan.nextInt(); 10 MSTree mst = new MSTree(n); 11 12 for (int y = 0; y < n; y++) 13 for (int x = 0; x < n; x++) 14 mst.setLeng(x, y, scan.nextInt()); 15 16 System.out.println(mst.getMinTree()); 17 18 scan.close(); 19 System.exit(0); 20 } 21} 22 23class MSTree { 24 int[][] leng; 25 int[] reach; 26 27 public MSTree(int n) { 28 leng = new int[n][n]; 29 reach = new int[n]; 30 for (int i = 0; i < n; i++) 31 reach[i] = -1; 32 } 33 34 public int getMinTree() { 35 int min = Integer.MAX_VALUE; 36 int xx = 0, yy = 0; 37 for (int x = 0; x < leng.length; x++) 38 for (int y = x + 1; y < leng[x].length; y++) 39 if (leng[x][y] !=-1 && leng[x][y] < min) { 40 xx = x; 41 yy = y; 42 min = leng[x][y]; 43 } 44 reach[xx] = 0; 45 reach[yy] = min; 46 for (int i = 2; i < reach.length; i++) 47 nextMin(); 48 49 int total = 0; 50 for (int i = 0; i < reach.length; i++) 51 total += reach[i]; 52 return total; 53 } 54 55 private void nextMin() { 56 int min = Integer.MAX_VALUE; 57 int xx = 0, yy = 0; 58 for (int x = 0; x < leng.length; x++) { 59 if (reach[x] == -1) 60 continue; 61 for (int y = 0; y < leng[x].length; y++) { 62 if (reach[y] != -1||leng[x][y]==-1) 63 continue; 64 if (leng[x][y] < min) { 65 xx = x; 66 yy = y; 67 min = leng[x][y]; 68 } 69 } 70 } 71 reach[yy] = min; 72 } 73 74 public void setLeng(int x, int y, int length) { 75 leng[x][y] = length; 76 } 77 78}
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。