Pagini recente » Cod sursa (job #2717541) | Cod sursa (job #3288595) | Cod sursa (job #1747795) | Cod sursa (job #1696220) | Cod sursa (job #2238835)
import static java.lang.Character.isDigit;
import static java.util.Arrays.fill;
import static java.util.Objects.requireNonNull;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
public final class Main {
public static final String IN_FILE = "dijkstra.in";
public static final String OUT_FILE = "dijkstra.out";
public static final int INFINITY = 0x3f3f3f3f;
public final static class FastScanner implements AutoCloseable {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream stream;
private final byte[] buffer = new byte[BUFFER_SIZE];
private int bufferPointer = 0;
private int bytesRead = 0;
public FastScanner(final String fileName) throws IOException {
stream = new DataInputStream(new FileInputStream(fileName));
}
public int nextInt() throws IOException {
byte c;
do {
c = read();
} while (c != '-' && !isDigit(c));
final boolean isNegative = c == '-';
if (isNegative) {
c = read();
}
int value = 0;
do {
value = value * 10 + (c - '0');
c = read();
} while (isDigit(c));
return isNegative ? -value : value;
}
private byte read() throws IOException {
final byte c = tryRead();
if (c == -1) {
throw new IOException("Reached end of stream!");
}
return c;
}
private byte tryRead() throws IOException {
if (bufferPointer == bytesRead) {
bufferPointer = 0;
bytesRead = stream.read(buffer, 0, BUFFER_SIZE);
if (bytesRead == -1) {
bytesRead = 0;
return -1;
}
}
return buffer[bufferPointer++];
}
@Override
public void close() throws IOException {
stream.close();
}
}
public static final class Edge {
public final int v;
public final int c;
public Edge(final int v, final int c) {
this.v = v;
this.c = c;
}
}
private static final class HeapNode implements Comparable<HeapNode> {
final int vertex;
final int cost;
HeapNode(final int vertex, final int cost) {
this.vertex = vertex;
this.cost = cost;
}
@Override
public int compareTo(final HeapNode other) {
return Integer.compare(cost, other.cost);
}
}
public static int[] dijkstra(final List<List<Edge>> graph, final int source) {
int[] distances = new int[graph.size()];
fill(distances, INFINITY);
final PriorityQueue<HeapNode> q = new PriorityQueue<>();
q.add(new HeapNode(source, 0));
while (!q.isEmpty()) {
final HeapNode top = q.poll();
final int u = top.vertex;
if (distances[u] != INFINITY) {
continue;
}
distances[u] = top.cost;
for (final Edge e : graph.get(u)) {
if (distances[e.v] == INFINITY) {
q.add(new HeapNode(e.v, distances[u] + e.c));
}
}
}
return distances;
}
public static List<List<Edge>> readGraph(final FastScanner scanner)
throws IOException {
final int size = scanner.nextInt();
final int edges = scanner.nextInt();
final List<List<Edge>> graph = new ArrayList<>(size);
for (int u = 0; u < size; u++) {
graph.add(new ArrayList<>());
}
for (int i = 1; i <= edges; i++) {
final int u = scanner.nextInt() - 1;
final int v = scanner.nextInt() - 1;
final int c = scanner.nextInt();
final Edge e = new Edge(v, c);
graph.get(u).add(e);
}
return graph;
}
public static void main(final String[] args) throws IOException {
try (final FastScanner scanner = new FastScanner(IN_FILE);
final PrintWriter writer = new PrintWriter(OUT_FILE)) {
final List<List<Edge>> graph = readGraph(scanner);
final int[] distances = dijkstra(graph, 0);
for (int u = 1; u < graph.size(); u++) {
writer.print(distances[u] == INFINITY ? 0 : distances[u]);
writer.print(u + 1 < graph.size() ? ' ' : '\n');
}
}
}
}