Pagini recente » Cod sursa (job #562534) | Cod sursa (job #2244378) | Cod sursa (job #438227) | Cod sursa (job #830594) | Cod sursa (job #2087401)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("dijkstra.in");
BufferedReader bufferedReader = new BufferedReader(fileReader);
Scanner scanner = new Scanner(bufferedReader);
int nodes = scanner.nextInt();
int noEdges = scanner.nextInt();
Map<Integer, Graph.Vertex> graph = new HashMap<>(noEdges);
for (int i = 0; i < noEdges; i++) {
int v1 = scanner.nextInt();
int v2 = scanner.nextInt();
int dist = scanner.nextInt();
if (!graph.containsKey(v1)) {
graph.put(v1, new Graph.Vertex(v1));
}
if (!graph.containsKey(v2)) {
graph.put(v2, new Graph.Vertex(v2));
}
graph.get(v1).neighbours.add(v2);
graph.get(v1).neighbours.add(dist);
}
bufferedReader.close();
Graph g = new Graph(graph);
g.dijkstra(1);
StringBuilder output = new StringBuilder();
for (int i = 2; i <= nodes; i++) {
Graph.Vertex vertex = g.graph.get(i);
if (vertex == null || vertex.dist == Integer.MAX_VALUE) {
output.append("0");
} else {
output.append(vertex.dist);
}
output.append(" ");
}
BufferedWriter writer = new BufferedWriter(new FileWriter("dijkstra.out"));
writer.write(output.toString());
writer.close();
}
}
class Graph {
public final Map<Integer, Vertex> graph;
public static class Vertex implements Comparable<Vertex> {
public final int id;
public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity
public Vertex previous = null;
public ArrayList<Integer> neighbours = new ArrayList<>();
public Vertex(int id) {
this.id = id;
}
public int compareTo(Vertex other) {
return Double.compare(dist, other.dist);
}
@Override
public String toString() {
return "(" + id + ", " + dist + ")";
}
}
public Graph(Map<Integer, Vertex> graph) {
this.graph = graph;
}
/**
* Runs dijkstra using a specified source vertex
*/
public void dijkstra(Integer startName) {
if (!graph.containsKey(startName)) {
System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName);
return;
}
final Vertex source = graph.get(startName);
source.dist = 0;
source.previous = source;
PriorityQueue<Vertex> heap = new PriorityQueue<Vertex>();
heap.add(source);
Vertex u, v;
int dist;
while (!heap.isEmpty()) {
u = heap.remove(); // vertex with shortest distance (first iteration will return source)
if (u.dist == Integer.MAX_VALUE) {
break;
// we can ignore u (and any other remaining vertices) since they are unreachable
}
//look at distances to each neighbour
for (int i = 0; i < u.neighbours.size(); i += 2) {
v = graph.get(u.neighbours.get(i));
dist = u.neighbours.get(i + 1);
int alternateDist = u.dist + dist;
if (alternateDist < v.dist) {
// shorter path to neighbour found
heap.remove(v);
v.dist = alternateDist;
v.previous = u;
heap.add(v);
}
}
}
}
}