forked from xtaci/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
172 lines (154 loc) · 5.11 KB
/
Copy pathSolution.java
File metadata and controls
172 lines (154 loc) · 5.11 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
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 {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
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]);
//if (i == 4) System.out.println(line);
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);
//if (i == 4) System.out.println(line);
}
//graph.printGraph();
startV = Integer.parseInt(ob.readLine()) -1;
//if (i == 4) System.out.println(""+startV);
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) {
//System.out.println("addEdge " + u + " to " + v + " weight = " + 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));
// List<Edge> adjacentEdge = adjacentList.get(u);
// if (u < v) {
// if (!removeIfExistLargerEdge(u,v, weight))
// adjacentList.get(u).add(new Edge(u , v, weight));
// } else {
// if (!removeIfExistLargerEdge(v,u, weight))
// adjacentLis®t.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);
}
}
}
}
/**
* 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;
// prev[i] = -1;
visited[i] = false;
}
}
public void findShortestPath(int source) {
this.source = source;
shortestDistance[source] = 0;
//prev[source] = -1;
visited[source] = true;
Queue<Integer> queue = new LinkedList<>();
queue.add(source);
while (!queue.isEmpty()) {
int from = queue.remove();
List<AdjacentListGraph.Edge> listEdge = graph.adjacentList.get(from);
for (AdjacentListGraph.Edge edge : listEdge) {
//System.out.println("Edge from " + edge.from + " to " + edge.to + " weight = " + edge.weight);
if (shortestDistance[edge.to] > shortestDistance[from] + edge.weight) {
shortestDistance[edge.to] = shortestDistance[from] + edge.weight;
//prev[edge.to] = from;
}
if (!visited[edge.to]) {
visited[edge.to] = true;
queue.add(edge.to);
}
}
}
// graph.printGraph();
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());
}
}
}