-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstraShortestReach2.java
More file actions
174 lines (153 loc) · 4.31 KB
/
DijkstraShortestReach2.java
File metadata and controls
174 lines (153 loc) · 4.31 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Map.Entry;
public class DijkstraShortestReach2 {
Graph g;
int[] distance;
HashSet<Integer> hsUnsettledNodes;
HashSet<Integer> hsSettledNodes;
int source;
public class Graph{
int V;
public int getV() {
return V;
}
List<HashMap<Integer,Integer>>[] aj;
@SuppressWarnings("unchecked")
public Graph(int V){
this.V = V;
aj=new List[V+1];
}
public void setAj(int src,int dest,int value){
HashMap<Integer,Integer> hm1= new HashMap<Integer, Integer>();
hm1.put(dest,value);
HashMap<Integer,Integer> hm2= new HashMap<Integer, Integer>();
hm2.put(src,value);
if(aj[src]==null){
aj[src]=new LinkedList<HashMap<Integer,Integer>>();
aj[src].add(hm1);
}
else{
aj[src].add(hm1);
}
if(aj[dest]==null){
aj[dest]=new LinkedList<HashMap<Integer,Integer>>();
aj[dest].add(hm2);
}
else{
aj[dest].add(hm2);
}
}
public String toString(){
StringBuilder sb=new StringBuilder();
for(int i=1;i<=V;i++){
sb.append("\n"+i);
List<HashMap<Integer,Integer>> list = aj[i];
Iterator<HashMap<Integer,Integer>> itr = list.iterator();
while(itr.hasNext()){
HashMap<Integer,Integer> hm = itr.next();
for(Entry<Integer,Integer> entry:hm.entrySet()){
int key=entry.getKey();
int value=entry.getValue();
sb.append("-->"+key+","+value);
}
}
}
return sb.toString();
}
}
public static void main(String[] args) {
DijkstraShortestReach2 dijkstraShortReach= new DijkstraShortestReach2();
Scanner in = new Scanner(System.in);
int t = in.nextInt();
String[] results=new String[t];
for(int a0 = 0; a0 < t; a0++){
int n = in.nextInt();
dijkstraShortReach.g= dijkstraShortReach.new Graph(n);
dijkstraShortReach.distance=new int[n+1];
dijkstraShortReach.initialize();
int m = in.nextInt();
for(int a1 = 0; a1 < m; a1++){
int x = in.nextInt();
int y = in.nextInt();
int r = in.nextInt();
dijkstraShortReach.g.setAj(x,y,r);
}
// System.out.println(dijkstraShortReach.g);
int s = in.nextInt();
dijkstraShortReach.source=s;
dijkstraShortReach.distance[s]=0;
dijkstraShortReach.execute();
results[a0] = dijkstraShortReach.buildResString();
}
printArray(results);
}
private String buildResString() {
StringBuilder sb = new StringBuilder();
for(int i=1;i<distance.length;i++){
if(i==source)
continue;
if(distance[i]==Integer.MAX_VALUE){
sb.append("-1 ");
continue;
}
sb.append(distance[i]+" ");
}
return sb.toString().substring(0, sb.toString().length()-1);
}
private void initialize() {
for(int i=1;i<=g.getV();i++)
distance[i]=Integer.MAX_VALUE;
hsUnsettledNodes=new HashSet<Integer>();
hsSettledNodes=new HashSet<Integer>();
}
private void execute() {
hsUnsettledNodes.add(source);
while(!hsUnsettledNodes.isEmpty()){
int node=getMinimumDistanceNode();
hsUnsettledNodes.remove(node);
hsSettledNodes.add(node);
evaluateNode(node);
}
}
private void evaluateNode(int node) {
List<HashMap<Integer,Integer>> list = g.aj[node];
Iterator<HashMap<Integer,Integer>> itr = list.iterator();
while(itr.hasNext()){
HashMap<Integer,Integer> hm = itr.next();
for(Entry<Integer,Integer> entry:hm.entrySet()){
int key=entry.getKey();
int value=entry.getValue();
if(!hsSettledNodes.contains(key)){
if(distance[key]>distance[node]+value){
distance[key]=distance[node]+value;
}
hsUnsettledNodes.add(key);
}
}
}
}
private int getMinimumDistanceNode() {
int minimum=0;
Iterator<Integer> itr= hsUnsettledNodes.iterator();
while(itr.hasNext()){
int tempNode = itr.next();
if(minimum==0)
minimum=tempNode;
else{
minimum=distance[minimum]>distance[tempNode]?tempNode:minimum;
}
}
return minimum;
}
private static void printArray(String[] results) {
int iLen = results.length;
for (int i = 0; i < iLen; i++) {
System.out.println(results[i]);
}
}
}