|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static final int INF = (int) 2e9 + 10; |
| 9 | + private static List<Edge>[] edges; |
| 10 | + private static int[][] dist; |
| 11 | + private static int V, E; |
| 12 | +
|
| 13 | + public static void main(String[] args) throws IOException { |
| 14 | + init(); |
| 15 | + dijkstra(1); |
| 16 | +
|
| 17 | + for (int i = 2; i <= V; i++) { |
| 18 | + bw.write(Math.min(dist[i][0], dist[i][1]) + "\n"); |
| 19 | + } |
| 20 | +
|
| 21 | + bw.flush(); |
| 22 | + bw.close(); |
| 23 | + br.close(); |
| 24 | + } |
| 25 | +
|
| 26 | + private static void init() throws IOException { |
| 27 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 28 | + V = Integer.parseInt(st.nextToken()); |
| 29 | + E = Integer.parseInt(st.nextToken()); |
| 30 | +
|
| 31 | + edges = new List[V+1]; |
| 32 | + dist = new int[V+1][2]; |
| 33 | +
|
| 34 | + for (int i = 1; i <= V; i++) { |
| 35 | + edges[i] = new ArrayList<>(); |
| 36 | + } |
| 37 | +
|
| 38 | + for (int i = 0; i < E; i++) { |
| 39 | + st = new StringTokenizer(br.readLine()); |
| 40 | + int node1 = Integer.parseInt(st.nextToken()); |
| 41 | + int node2 = Integer.parseInt(st.nextToken()); |
| 42 | + int cost = Integer.parseInt(st.nextToken()); |
| 43 | + int taste = Integer.parseInt(st.nextToken()); |
| 44 | +
|
| 45 | + edges[node1].add(new Edge(node2, cost, taste)); |
| 46 | + edges[node2].add(new Edge(node1, cost, taste)); |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + private static void dijkstra(int start) { |
| 51 | + PriorityQueue<Edge> pq = new PriorityQueue<>((o1, o2) -> Integer.compare(o1.cost, o2.cost)); |
| 52 | + for (int i = 1; i <= V; i++) { |
| 53 | + dist[i][0] = INF; |
| 54 | + dist[i][1] = INF; |
| 55 | + } |
| 56 | + dist[start][0] = 0; |
| 57 | + pq.add(new Edge(1, 0, false)); |
| 58 | +
|
| 59 | + while (!pq.isEmpty()) { |
| 60 | + Edge current = pq.poll(); |
| 61 | +
|
| 62 | + if (current.eat && current.cost > dist[current.dest][1]) continue; |
| 63 | + if (!current.eat && current.cost > dist[current.dest][0]) continue; |
| 64 | +
|
| 65 | + for (Edge next : edges[current.dest]) { |
| 66 | + int nCost = current.cost + next.cost; |
| 67 | + if (!current.eat && nCost < dist[next.dest][0]) { |
| 68 | + dist[next.dest][0] = nCost; |
| 69 | + pq.add(new Edge(next.dest, nCost, false)); |
| 70 | + } |
| 71 | + if (current.eat && nCost < dist[next.dest][1]) { |
| 72 | + dist[next.dest][1] = nCost; |
| 73 | + pq.add(new Edge(next.dest, nCost, true)); |
| 74 | + } |
| 75 | + nCost -= next.taste; |
| 76 | + if (!current.eat && nCost < dist[next.dest][1]) { |
| 77 | + dist[next.dest][1] = nCost; |
| 78 | + pq.add(new Edge(next.dest, nCost, true)); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + static class Edge { |
| 85 | + int dest; |
| 86 | + int cost; |
| 87 | + int taste; |
| 88 | + boolean eat; |
| 89 | +
|
| 90 | + public Edge(int dest, int cost, int taste) { |
| 91 | + this.dest = dest; |
| 92 | + this.cost = cost; |
| 93 | + this.taste = taste; |
| 94 | + this.eat = false; |
| 95 | + } |
| 96 | +
|
| 97 | + public Edge(int dest, int cost, boolean eat) { |
| 98 | + this.dest = dest; |
| 99 | + this.cost = cost; |
| 100 | + this.eat = eat; |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
0 commit comments