結果

問題 No.17 2つの地点に泊まりたい
ユーザー k82b
提出日時 2023-10-10 21:33:19
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 352 ms / 5,000 ms
コード長 1,797 bytes
コンパイル時間 3,798 ms
コンパイル使用メモリ 187,764 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2025-01-01 18:36:00
合計ジャッジ時間 5,031 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

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