結果

問題 No.17 2つの地点に泊まりたい
ユーザー k82bk82b
提出日時 2023-10-10 21:33:19
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 403 ms / 5,000 ms
コード長 1,797 bytes
コンパイル時間 1,799 ms
コンパイル使用メモリ 182,244 KB
実行使用メモリ 9,956 KB
最終ジャッジ日時 2023-10-10 21:33:40
合計ジャッジ時間 6,116 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 105 ms
5,896 KB
testcase_04 AC 22 ms
4,352 KB
testcase_05 AC 222 ms
9,736 KB
testcase_06 AC 116 ms
7,540 KB
testcase_07 AC 71 ms
6,100 KB
testcase_08 AC 403 ms
9,548 KB
testcase_09 AC 402 ms
9,956 KB
testcase_10 AC 200 ms
7,472 KB
testcase_11 AC 295 ms
8,976 KB
testcase_12 AC 1 ms
4,352 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 7 ms
4,348 KB
testcase_18 AC 61 ms
5,564 KB
testcase_19 AC 31 ms
5,384 KB
testcase_20 AC 5 ms
4,352 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 87 ms
5,844 KB
testcase_23 AC 188 ms
9,780 KB
testcase_24 AC 224 ms
8,308 KB
testcase_25 AC 4 ms
4,352 KB
testcase_26 AC 333 ms
9,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

void main()
{
	enum INF = 10 ^^ 9;
	alias Entry = Tuple!(int, "c", int, "u");
	const N = readInt;
	auto S = new int[N];
	foreach (i; 0 .. N) S[i] = readInt;
	const M = readInt;
	auto A = new int[M];
	auto B = new int[M];
	auto C = new int[M];
	auto G = new int[][](N, 0);
	foreach (i; 0 .. M) {
		A[i] = readInt;
		B[i] = readInt;
		C[i] = readInt;
		G[A[i]] ~= i;
		G[B[i]] ~= i;
	}
	int ans = INF;
	foreach (i; 1 .. N - 1) foreach (j; 1 .. N - 1) {
		if (i == j) continue;
		int now = S[i] + S[j];
		bool ok = true;
		foreach (V; [[0, i], [i, j], [j, N - 1]]) {
			auto dist = new int[N];
			auto Q = new BinaryHeap!(Array!Entry, "a > b");
			dist[] = INF;
			dist[V[0]] = 0;
			Q.insert(Entry(0, V[0]));
			while (!Q.empty) {
				int c = Q.front.c;
				int u = Q.front.u;
				Q.removeFront;
				if (dist[u] < c) continue;
				foreach (k; G[u]) {
					int v = u ^ A[k] ^ B[k];
					if (chmin(dist[v], c + C[k])) {
						Q.insert(Entry(dist[v], v));
					}
				}
			}
			if (dist[V[1]] == INF) {
				ok = false;
				break;
			}
			now += dist[V[1]];
		}
		if (ok) chmin(ans, now);
	}
	ans.writeln;
}

import std,core.bitop;
string[]_R;
string readString(){while(_R.empty){_R=readln.chomp.split;}auto ret=_R.front;_R.popFront;return ret;}
int readInt(){return readString.to!int;}
long readLong(){return readString.to!long;}
ulong readULong(){return readString.to!ulong;}
real readReal(){return readString.to!real;}
bool chmin(T)(ref T A,T B){if(A>B){A=B;return true;}else{return false;}}
bool chmax(T)(ref T A,T B){if(A<B){A=B;return true;}else{return false;}}
int lowerBound(T)(T[]A,T x){int L=-1,R=cast(int)A.length;while(R-L>1){int mid=(L+R)/2;(A[mid]<x?L:R)=mid;}return R;}
int upperBound(T)(T[]A,T x){int L=-1,R=cast(int)A.length;while(R-L>1){int mid=(L+R)/2;(A[mid]<=x?L:R)=mid;}return R;}
0