###前提・実現したいこと
これは何をしているのですか?
elem.get(1).set(elem.get(elem.size()-1));
###発生している問題・エラーメッセージ
エラーメッセージ
###該当のソースコード
java
1import java.util.*; 2 3public class Algo44{ 4 5 public static void main(String[] args){ 6 7 Scanner scan = new Scanner(System.in); 8 int n = scan.nextInt(); 9 SSSPath sp = new SSSPath(n); 10 11 for(int i = 0; i < n; i++){ 12 int u = scan.nextInt(); 13 int k = scan.nextInt(); 14 for(int j = 0; j < k; j++) 15 sp.addPath(u, scan.nextInt(), scan.nextInt()); 16 } 17 sp.setPath(0); 18 19 for(int i = 0; i < n; i++) 20 System.out.println(i + " " + sp.getResult(i)); 21 22 scan.close(); 23 System.exit(0); 24 } 25} 26 27class Bheap{ 28 class Elem{ 29 int key; 30 int pt; 31 32 public Elem(int _pt, int _key){ 33 pt = _pt; 34 key = _key; 35 } 36 37 public void set(Elem e){ 38 pt = e.pt; 39 key = e.key; 40 } 41 } 42 43 List<Elem> elem = new ArrayList<Elem>(); 44 45 public Bheap(){ 46 elem.add(new Elem(-1, -1)); 47 } 48 49 public int getTop(){ 50 if(elem.size() <= 1) 51 return -1; 52 int rpt = elem.get(1).pt; 53 54 elem.get(1).set(elem.get(elem.size()-1)); 55 elem.remove(elem.size()-1); 56 57 this.down(1); 58 return rpt; 59 } 60 private void down(int c){ 61 int min = c; 62 if(c * 2 < elem.size() && elem.get(c * 2).key < elem.get(min).key) 63 min = c * 2; 64 if(c * 2 + 1 < elem.size() && elem.get(c * 2 + 1).key < elem.get(min).key) 65 min = c * 2 + 1; 66 if(min == c) 67 return; 68 this.swap(min, c); 69 this.down(min); 70 } 71 private void swap(int c1, int c2){ 72 Elem temp = new Elem(-1, -1); 73 temp.set(elem.get(c1)); 74 elem.get(c1).set(elem.get(c2)); 75 elem.get(c2).set(temp); 76 } 77 78 public void add(int ptr, int cost){ 79 elem.add(new Elem(ptr, cost)); 80 this.up(elem.size()-1); 81 } 82 private void up(int c){ 83 if(c < 2 || elem.get(c).key >= elem.get(c / 2).key) 84 return; 85 86 swap(c, c / 2); 87 this.up(c / 2); 88 } 89} 90 91class SSSPath{ 92 class Next{ 93 int pt; 94 int cost; 95 96 public Next(int to, int c){ 97 pt = to; 98 cost = c; 99 } 100 } 101 102 class Point{ 103 List<Next> next = new ArrayList<Next>(); 104 int reach = -1; 105 boolean fixed = false; 106 } 107 108 Point[] point; 109 Bheap bheap; 110 111 public SSSPath(int n){ 112 point = new Point[n]; 113 for(int i = 0; i < n; i++) 114 point[i] = new Point(); 115 116 bheap = new Bheap(); 117 } 118 119 public void addPath(int from, int to, int c){ 120 point[from].next.add(new Next(to, c)); 121 } 122 123 public void setPath(int from){ 124 point[from].reach = 0; 125 bheap.add(from, 0); 126 setP(); 127 } 128 private void setP(){ 129 int p; 130 while((p = bheap.getTop()) != -1){ 131 if(point[p].fixed) 132 continue; 133 point[p].fixed = true; 134 for(int i = 0; i < point[p].next.size(); i++){ 135 int np = point[p].next.get(i).pt; 136 if(point[np].fixed) 137 continue; 138 int d = point[p].reach + point[p].next.get(i).cost; 139 if(point[np].reach == -1 || point[np].reach > d){ 140 point[np].reach = d; 141 bheap.add(np, d); 142 } 143 } 144 } 145 } 146 147 public int getResult(int i){ 148 return point[i].reach; 149 } 150}
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
違うと考える理由を具体的に説明してください。
回答2件
あなたの回答
tips
プレビュー