forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijikstraSP2.java
More file actions
172 lines (155 loc) · 4.57 KB
/
Copy pathDijikstraSP2.java
File metadata and controls
172 lines (155 loc) · 4.57 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
//Problem: https://www.hackerrank.com/challenges/dijkstrashortreach/submissions/code/14904366
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader ob=new BufferedReader(new InputStreamReader(System.in));
int testCases,nodes,edges,startV;
int from, to,weight;
String line = ob.readLine();
testCases = Integer.parseInt(line);
for(int i=0;i<testCases;i++){
line=ob.readLine();
String [] array = line.split(" ");
nodes = Integer.parseInt(array[0]);
AdjacentListGraph graph = new AdjacentListGraph(nodes);
edges = Integer.parseInt(array[1]);
for (int j = 0; j < edges; j++) {
line = ob.readLine();
array = line.split(" ");
from = Integer.parseInt(array[0]) -1;
to = Integer.parseInt(array[1]) -1;
weight = Integer.parseInt(array[2]);
graph.addEdge(from, to, weight);
}
startV = Integer.parseInt(ob.readLine())-1;
DijkstraAdjacentList dijkstra = new DijkstraAdjacentList(graph);
dijkstra.findShortestPath(startV);
}
}
public static class AdjacentListGraph {
public class Edge {
int from;
int to;
int weight;
public Edge(int from, int to, int weight){
this.from = from;
this.to = to;
this.weight = weight;
}
}
Map<Integer, List<Edge>> adjacentList;
int V;
public AdjacentListGraph (int V) {
this.V = V;
adjacentList = new HashMap<>();
for (int i = 0; i < this.V; i ++) {
adjacentList.put(i, new LinkedList<Edge>());
}
}
private boolean removeIfExistLargerEdge(int u, int v, int weight) {
List<Edge> adjacentEdge;
adjacentEdge = adjacentList.get(u);
for(Edge edge : adjacentEdge){
if (edge.from == u && edge.to == v) {
if (weight < edge.weight)
edge.weight = weight;
return true;
}
}
return false;
}
public void addEdge(int u, int v, int weight) {
if (!removeIfExistLargerEdge(u,v, weight))
adjacentList.get(u).add(new Edge(u , v, weight));
if (!removeIfExistLargerEdge(v,u, weight))
adjacentList.get(v).add(new Edge(v, u, weight));
}
public void printGraph(){
for (int vertex = 0; vertex < V; vertex ++ ) {
List<Edge> adjacentEdge = adjacentList.get(vertex);
for (Edge edge: adjacentEdge) {
System.out.println("Edge from " + edge.from + " to " + edge.to + " weight = " + edge.weight);
}
}
}
}
public static class Vertex implements Comparable<Vertex> {
public int minDistance = Integer.MAX_VALUE;
public Vertex previous;
int name;
public Vertex(int name, int minDistance) {
this.name = name;
this.minDistance = minDistance;
}
@Override
public int compareTo(Vertex other) {
return Integer.compare(minDistance, other.minDistance);
}
}
/**
* Created by loinguyen on 26/10/2015.
*/
public static class DijkstraAdjacentList {
AdjacentListGraph graph;
int [] shortestDistance;
//int [] prev;
int V;
int source;
boolean [] visited;
public DijkstraAdjacentList (AdjacentListGraph graph){
this.graph = graph;
this.V = graph.V;
shortestDistance = new int[V];
//prev = new int[V];
visited = new boolean[V];
for (int i = 0; i < V; i++){
shortestDistance[i] = Integer.MAX_VALUE;
visited[i] = false;
}
}
public void findShortestPath(int source) {
this.source = source;
shortestDistance[source] = 0;
PriorityQueue<Vertex> queue = new PriorityQueue<Vertex>();
queue.add(new Vertex(source, 0));
while (!queue.isEmpty()) {
Vertex from = queue.poll();
List<AdjacentListGraph.Edge> listEdge = graph.adjacentList.get(from.name);
for (AdjacentListGraph.Edge edge : listEdge) {
if (shortestDistance[edge.to] > shortestDistance[from.name] + edge.weight) {
shortestDistance[edge.to] = shortestDistance[from.name] + edge.weight;
boolean isAdded = false;
for (Vertex item : queue) {
if (item.name == edge.to) {
isAdded = true;
break;
}
}
if (!isAdded) {
queue.add(new Vertex(edge.to, shortestDistance[edge.to]));
}
}
}
visited[from.name] = true;
}
printShortestPath();
}
public void printShortestPath(){
StringBuilder builder = new StringBuilder();
for (int vertex = 0; vertex < V; vertex++ ) {
if (vertex != this.source) {
if (shortestDistance[vertex] == Integer.MAX_VALUE) shortestDistance[vertex] = -1;
if (vertex == V-1)
builder.append(shortestDistance[vertex]);
else
builder.append(shortestDistance[vertex]).append(" ");
}
}
System.out.println(builder.toString());
}
}
}