-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlostmap.java
More file actions
82 lines (68 loc) · 2.25 KB
/
lostmap.java
File metadata and controls
82 lines (68 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.*;
import java.io.*;
class IntegerTriple implements Comparable<IntegerTriple> {
public Integer _source, _destination, _weight;
public IntegerTriple(Integer s, Integer d, Integer w) {
_source = s;
_destination = d;
_weight = w;
}
public int compareTo(IntegerTriple o) {
if (!this.weight().equals(o.weight()))
return this.weight() - o.weight();
else
return this.dest() - o.dest();
}
Integer source() { return _source; }
Integer dest() { return _destination; }
Integer weight() { return _weight; }
}
class lostmap {
public static ArrayList < ArrayList < IntegerTriple > > AdjList;
public static ArrayList < Boolean > taken;
public static PriorityQueue < IntegerTriple > pq;
public static void process(int vtx) {
taken.set(vtx, true);
for (int j = 0; j < AdjList.get(vtx).size(); j++) {
IntegerTriple v = AdjList.get(vtx).get(j);
if (!taken.get(v.dest())) {
pq.offer(new IntegerTriple(vtx, v.dest(), v.weight()));
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
Integer V = Integer.parseInt(br.readLine());
AdjList = new ArrayList < ArrayList < IntegerTriple > >();
Integer[][] distance = new Integer[V][V];
for (int i = 0; i < V; i ++) {
String[] input = br.readLine().split(" ");
for (int j = 0; j < V; j++) {
distance[i][j] = Integer.parseInt(input[j]);
}
}
for (int i = 0; i < V; i ++) {
ArrayList < IntegerTriple > Neighbor = new ArrayList < IntegerTriple >();
AdjList.add(Neighbor);
}
for (int i = 0; i < V; i ++) {
for (int j = 0; j < V; j++) {
AdjList.get(i).add(new IntegerTriple(i,j,distance[i][j]));
}
}
taken = new ArrayList < Boolean >(); taken.addAll(Collections.nCopies(V, false));
pq = new PriorityQueue < IntegerTriple > ();
process(0);
while (!pq.isEmpty()) {
IntegerTriple front = pq.poll();
if (!taken.get(front.dest())) {
Integer s = front.source() + 1;
Integer d = front.dest() +1 ;
pw.println(s + " " + d);
process(front.dest());
}
}
pw.close();
}
}