-
Notifications
You must be signed in to change notification settings - Fork 0
/
EqDist.java
96 lines (94 loc) · 3.29 KB
/
EqDist.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.util.*;
public class EqDist {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int numv = in.nextInt();
in.nextLine();
int numr = in.nextInt();
in.nextLine();
List<List<List<Integer>>> grath = new ArrayList<>();
for (int i = 0; i < numv; i++) {
grath.add(new ArrayList<>());
}
for (int i = 0; i < numr; i++) {
var a = in.nextInt();
var b = in.nextInt();
var c = in.nextInt();
in.nextLine();
grath.get(a).add(List.of(b, c));
grath.get(b).add(List.of(a, c));
}
int numop = in.nextInt();
in.nextLine();
List <Integer> opvert = new ArrayList<>();
for (int i = 0; i < numop; i++){
opvert.add(in.nextInt());
in.nextLine();
}
List<Integer> res = new ArrayList<>();
for (var o : opvert) {
Dictionary<Integer, Integer> ways = new Hashtable<>();
Dictionary<Integer, Boolean> marks = new Hashtable<>();
for (int i = 0; i < numv; i++) {
if (i == o) {
ways.put(i, 0);
} else {
ways.put(i, Integer.MAX_VALUE);
}
marks.put(i, false);
}
boolean flag = true;
while (flag) {
var c = ways.keys();
int minway = Integer.MAX_VALUE;
int minvert = 0;
while (c.hasMoreElements()) {
var d = c.nextElement();
if (!marks.get(d) && ways.get(d) < minway) {
minway = ways.get(d);
minvert = d;
}
}
for (int i = 0; i < grath.get(minvert).size(); i++) {
int m = Math.min(ways.get(grath.get(minvert).get(i).get(0)), grath.get(minvert).get(i).get(1) + minway);
ways.put(grath.get(minvert).get(i).get(0), m);
}
marks.put(minvert, true);
flag = false;
c = ways.keys();
while (c.hasMoreElements()) {
var d = c.nextElement();
if (!marks.get(d)) {
flag = true;
break;
}
}
}
Dictionary<Integer, List<Integer>> counter = new Hashtable<>();
var c = ways.keys();
while (c.hasMoreElements()) {
var d = c.nextElement();
if (counter.get(ways.get(d)) != null){
counter.get(ways.get(d)).add(d);
} else {
counter.put(ways.get(d), new ArrayList<>(List.of(d)));
}
}
c = counter.keys();
while (c.hasMoreElements()) {
var d = c.nextElement();
if (counter.get(d).size() > 1){
res.addAll(counter.get(d));
}
}
}
res.sort(Comparator.naturalOrder());
if (res.isEmpty()){
System.out.println("-");
} else {
for (var y : res){
System.out.println(y);
}
}
}
}