ホーム BOJ. 最小費用の求め 2 (11779)
記事
キャンセル

BOJ. 最小費用の求め 2 (11779)

問題: BOJ 11779 — 最小費用の求め 2

辺の重みが非負の有向グラフで、指定された始点から終点までの最小費用と、その費用を実現する経路を一つ求めます。各有向辺は隣接リストに格納します。平行辺も有効であり、特別な処理をせずすべて保持できます。

ダイクストラ法

distance[v] は始点から v までに見つかった最小費用、parent[v] はその距離を最後に更新した経路での直前の頂点です。優先度付きキューには (頂点, 距離) の候補を格納します。最小の候補を取り出したとき、その距離が現在の distance[頂点] と異なれば、より良い経路に置き換えられた古い候補なので破棄します。そうでなければ、その頂点から出るすべての辺を緩和し、隣接頂点までの距離が短くなる場合は新たな候補をキューに追加します。

不変条件は、配列に記録された距離が実際に見つかった経路の費用であり、キュー内の各要素も経路候補を表すことです。すべての辺の重みが非負なので、現在有効な距離のうち最小の状態を取り出した後、未処理の頂点を経由する経路によってその距離がさらに小さくなることはありません。したがって、緩和を繰り返すことで到達可能な各頂点の最小距離が得られます。距離を更新するたびに、その経路の直前の頂点を親として記録します。終点から始点まで親をたどると最小費用経路を逆順に復元でき、それを反転すると必要な順序になります。始点と終点が同じ場合、経路にはその頂点だけが含まれます。

距離と辺の重みには long を使い、優先度付きキューの比較には減算ではなく Long.compare を使います。これにより比較時のオーバーフローを避け、距離の加算も long で行えます。出力はちょうど3行で、最小費用、経路に含まれる頂点数、経路上の頂点を順に出力します。

遅延削除を使う二分ヒープのダイクストラ法では、時間計算量は O((V + E) log(E + 1))、空間計算量は O(V + E) です。単純グラフでは時間計算量を一般に O((V + E) log(V + 1)) と表します。平行辺も含む一般的な場合には、上記の log(E + 1) の境界が適用できます。

Java

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
import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.PriorityQueue;

public class Main {
    private static final long INF = Long.MAX_VALUE / 4;

    private static class Edge {
        final int to;
        final long cost;

        Edge(int to, long cost) {
            this.to = to;
            this.cost = cost;
        }
    }

    private static class State {
        final int vertex;
        final long distance;

        State(int vertex, long distance) {
            this.vertex = vertex;
            this.distance = distance;
        }
    }

    public static void main(String[] args) throws IOException {
        FastScanner input = new FastScanner();
        int vertexCount = input.nextInt();
        int edgeCount = input.nextInt();

        List<List<Edge>> graph = new ArrayList<>(vertexCount);
        for (int vertex = 0; vertex < vertexCount; vertex++) {
            graph.add(new ArrayList<>());
        }

        for (int i = 0; i < edgeCount; i++) {
            int from = input.nextInt() - 1;
            int to = input.nextInt() - 1;
            long cost = input.nextLong();
            graph.get(from).add(new Edge(to, cost));
        }

        int source = input.nextInt() - 1;
        int destination = input.nextInt() - 1;

        long[] distance = new long[vertexCount];
        int[] parent = new int[vertexCount];
        Arrays.fill(distance, INF);
        Arrays.fill(parent, -1);
        distance[source] = 0;

        PriorityQueue<State> queue = new PriorityQueue<>(
                (left, right) -> Long.compare(left.distance, right.distance));
        queue.add(new State(source, 0));

        while (!queue.isEmpty()) {
            State current = queue.poll();
            if (current.distance != distance[current.vertex]) {
                continue;
            }

            for (Edge edge : graph.get(current.vertex)) {
                long candidate = current.distance + edge.cost;
                if (candidate < distance[edge.to]) {
                    distance[edge.to] = candidate;
                    parent[edge.to] = current.vertex;
                    queue.add(new State(edge.to, candidate));
                }
            }
        }

        List<Integer> path = new ArrayList<>();
        for (int vertex = destination; vertex != -1; vertex = parent[vertex]) {
            path.add(vertex);
            if (vertex == source) {
                break;
            }
        }
        java.util.Collections.reverse(path);

        StringBuilder output = new StringBuilder();
        output.append(distance[destination]).append('\n');
        output.append(path.size()).append('\n');
        for (int i = 0; i < path.size(); i++) {
            if (i > 0) {
                output.append(' ');
            }
            output.append(path.get(i) + 1);
        }
        output.append('\n');
        System.out.print(output);
    }

    private static class FastScanner {
        private final BufferedInputStream input = new BufferedInputStream(System.in);
        private final byte[] buffer = new byte[1 << 16];
        private int position;
        private int length;

        private int read() throws IOException {
            if (position == length) {
                length = input.read(buffer);
                position = 0;
                if (length == -1) {
                    return -1;
                }
            }
            return buffer[position++];
        }

        private long nextLong() throws IOException {
            int c;
            do {
                c = read();
            } while (c <= ' ' && c != -1);

            long value = 0;
            while (c > ' ') {
                value = value * 10 + c - '0';
                c = read();
            }
            return value;
        }

        private int nextInt() throws IOException {
            return (int) nextLong();
        }
    }
}
この記事は著者により CC BY 4.0 ライセンスで公開されています。