結果

問題 No.114 遠い未来
ユーザー 👑 hos.lyrichos.lyric
提出日時 2015-01-06 21:36:56
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,643 ms / 5,000 ms
コード長 3,720 bytes
コンパイル時間 5,306 ms
コンパイル使用メモリ 166,148 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-09-02 19:33:57
合計ジャッジ時間 14,759 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 1,643 ms
4,372 KB
testcase_02 AC 411 ms
4,368 KB
testcase_03 AC 108 ms
4,372 KB
testcase_04 AC 1 ms
4,372 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 1,346 ms
4,368 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 2 ms
4,368 KB
testcase_09 AC 599 ms
4,368 KB
testcase_10 AC 1,145 ms
4,368 KB
testcase_11 AC 555 ms
4,372 KB
testcase_12 AC 655 ms
4,368 KB
testcase_13 AC 324 ms
4,368 KB
testcase_14 AC 611 ms
4,372 KB
testcase_15 AC 172 ms
4,368 KB
testcase_16 AC 279 ms
4,368 KB
testcase_17 AC 88 ms
4,372 KB
testcase_18 AC 193 ms
4,368 KB
testcase_19 AC 100 ms
4,372 KB
testcase_20 AC 45 ms
4,368 KB
testcase_21 AC 9 ms
4,368 KB
testcase_22 AC 5 ms
4,368 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 3 ms
4,368 KB
testcase_25 AC 3 ms
4,368 KB
testcase_26 AC 2 ms
4,368 KB
testcase_27 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import core.thread;
import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.range, std.regex;
import core.bitop;

//	Input
class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { tokens = readln.split; if (stdin.eof) throw new EOFException; } auto token = tokens[0]; tokens.popFront; return token; }
int readInt() { return to!int(readToken); }
long readLong() { return to!long(readToken); }
real readReal() { return to!real(readToken); }

//	chmin/chmax
void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

//	Pair
struct Pair(S, T) {
	S x; T y;
	int opCmp(    const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	int opCmp(ref const Pair p) const { return (x < p.x) ? -1 : (x > p.x) ? +1 : (y < p.y) ? -1 : (y > p.y) ? +1 : 0; }
	string toString() const { return "(" ~ to!string(x) ~ ", " ~ to!string(y) ~ ")"; }
}
auto pair(S, T)(inout(S) x, inout(T) y) { return Pair!(S, T)(x, y); }

//	Array
int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = as.length; for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <  val); }); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) { return (a <= val); }); }
T[] unique(T)(in T[] as) { T[] bs; foreach (a; as) if (bs.empty || bs[$ - 1] != a) bs ~= a; return bs; }


int root(int[] uf, int u) {
	return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool conn(int[] uf, int u, int v) {
	u = uf.root(u);
	v = uf.root(v);
	if (u == v) return false;
	if (uf[u] > uf[v]) swap(u, v);
	uf[u] += uf[v];
	uf[v] = u;
	return true;
}

int N, M, K;
int[] A, B, C;
int[] T;

void main(string[] args) {
	try {
	for (; ; ) {
		N = readInt;
		M = readInt;
		K = readInt;
		A = new int[M];
		B = new int[M];
		C = new int[M];
		foreach (i; 0 .. M) {
			A[i] = readInt - 1;
			B[i] = readInt - 1;
			C[i] = readInt;
		}
		T = new int[K];
		foreach (k; 0 .. K) {
			T[k] = readInt - 1;
		}
		
		bool[] isT = new bool[N];
		foreach (t; T) {
			isT[t] = true;
		}
		int[] nonTs;
		foreach (u; 0 .. N) if (!isT[u]) {
			nonTs ~= u;
		}
		
		int[][] D = new int[][](N, N);
		foreach (u; 0 .. N) {
			D[u][] = int.max / 2;
			D[u][u] = 0;
		}
		foreach (i; 0 .. M) {
			chmin(D[A[i]][B[i]], C[i]);
			chmin(D[B[i]][A[i]], C[i]);
		}
		foreach (w; 0 .. N) foreach (u; 0 .. N) foreach (v; 0 .. N) {
			chmin(D[u][v], D[u][w] + D[w][v]);
		}
		
		if (K == 1) {
			writeln(0);
			continue;
		} else if (K == 2) {
			writeln(D[T[0]][T[1]]);
			continue;
		}
		
		Pair!(int, Pair!(int, int))[] edges;
		foreach (u; 0 .. N) foreach (v; u + 1 .. N) {
			edges ~= pair(D[u][v], pair(u, v));
		}
		edges.sort();
debug{
writeln("edges = ",edges);
}
		
		int ans = int.max;
		foreach (L; 0 .. K - 1) if (L <= N - K) {
			bool[] flg = new bool[N - K];
			flg[$ - L .. $] = true;
			do {
				bool[] on = isT.dup;
				foreach (j, u; nonTs) {
					if (flg[j]) {
						on[u] = true;
					}
				}
				int sum;
				int cnt = 1;
				int[] uf = new int[N];
				uf[] = -1;
				foreach (e; edges) {
					if (ans <= sum + (K + L - cnt) * e.x) {
						goto failed;
					}
					if (on[e.y.x] && on[e.y.y]) {
						if (uf.conn(e.y.x, e.y.y)) {
							sum += e.x;
							if (++cnt == K + L) {
								break;
							}
						}
					}
				}
				if (cnt == K + L) {
debug{
writeln(flg," ",sum);
}
					chmin(ans, sum);
				}
			failed:
			} while (flg.nextPermutation);
		}
		writeln(ans);
	}
	} catch (EOFException) {}
}
0