結果
| 問題 | No.2328 Build Walls |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-06-09 12:31:26 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 4,167 bytes |
| 記録 | |
| コンパイル時間 | 1,590 ms |
| コンパイル使用メモリ | 188,844 KB |
| 実行使用メモリ | 158,728 KB |
| 最終ジャッジ日時 | 2024-06-22 18:05:24 |
| 合計ジャッジ時間 | 7,372 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 10 TLE * 1 -- * 23 |
ソースコード
import std;
// ダイクストラ
// (H-2)*(W+1)の頂点をおいて、壁として成立するように有向辺をつなぐ
void read(T...)(string S, ref T args) {
auto buf = S.split;
foreach (i, ref arg; args) {
arg = buf[i].to!(typeof(arg));
}
}
void read_array(T)(string S, ref T[] arg) {
arg = S.split.to!(T[]);
}
void main () {
int H, W; readln.read(H, W);
int[][] A = new int[][](H-2, W);
foreach (i; 0..H-2) {
readln.read_array(A[i]);
}
solve(H, W, A);
}
struct pair {
int x;
int y;
}
void solve (int H, int W, int[][] A) {
auto G = Dijkstra!pair();
foreach (X; 0..W) {
foreach (Y; 0..H-2) {
if (0 <= Y-1 && A[Y-1][X] != -1) {
G.input(pair(X, Y), pair(X+1, Y-1), A[Y-1][X]);
}
if (A[Y][X] != -1) {
G.input(pair(X, Y), pair(X+1, Y), A[Y][X]);
}
if (Y+1 < H-2 && A[Y+1][X] != -1) {
G.input(pair(X, Y), pair(X+1, Y+1), A[Y+1][X]);
}
}
}
long ans = long.max;
foreach (Y; 0..H-2) {
G.calculate(pair(0, Y));
foreach (Y2; 0..H-2) {
ans = min(ans, G.cost(pair(W, Y2)));
}
}
if (ans == long.max) {
writeln(-1);
} else {
writeln(ans);
}
}
struct Dijkstra(T) {
T[][T] graph;
long[T][T] weight;
struct dijkstra_pair {
long distance;
T path;
T vertex;
}
dijkstra_pair[T] node;
private bool is_calculated = false;
private bool[T] is_comfirmed;
// 頂点の型Tであるグラフの辺と重みを受け取る
void input (T u, T v, long w) {
graph[u] ~= v;
weight[u][v] = w;
node[u] = dijkstra_pair(long.max, u, u);
node[v] = dijkstra_pair(long.max, v, v);
is_comfirmed[u] = false;
is_comfirmed[v] = false;
}
// ダイクストラのアルゴリズムに基づいて頂点startからの(連結成分の)最小重みを求める
void calculate (T start) {
if (start !in node) {
stderr.writeln("Fatal error in dijkstra.calculate(T start) : This vertex is not in the graph!");
return;
}
// 以前の計算のリセット
if (is_calculated) {
foreach (ref x; node) {
x.distance = long.max;
x.path = x.vertex;
}
}
node[start].distance = 0;
node[start].path = start;
// 確定済みかどうかをリセット
foreach (ref x; is_comfirmed) {
x = false;
}
// 優先度付きキューの宣言
BinaryHeap!(Array!dijkstra_pair, "b.distance < a.distance") PQueue;
PQueue.insert(node[start]);
while (!PQueue.empty) {
auto begin = PQueue.front; PQueue.removeFront;
if (is_comfirmed[begin.vertex]) {
continue;
}
is_comfirmed[begin.vertex] = true;
if (begin.vertex in graph) {
foreach (ref x; graph[begin.vertex]) {
if (node[begin.vertex].distance + weight[begin.vertex][x] < node[x].distance) {
node[x].distance = node[begin.vertex].distance + weight[begin.vertex][x];
node[x].path = begin.vertex;
PQueue.insert(node[x]);
}
}
}
}
// 連結でない頂点も計算済みということにしておく
foreach (ref x; is_comfirmed) {
x = true;
}
is_calculated = true;
}
// 頂点endへの最小重みを出力 パスが存在しなければlong.maxを返す。
long cost (T end) {
if (end !in node) {
stderr.writeln("Fatal error in dijkstra.cost(T end) : This vertex is not in the graph!");
return long.max;
}
if (!is_calculated) {
stderr.writeln("Costs are not calculatd. Do \"dijkstra.calculate(T start)\" first.");
return long.max;
}
return node[end].distance;
}
}