結果

問題 No.17 2つの地点に泊まりたい
ユーザー iicafiaxusiicafiaxus
提出日時 2018-12-09 18:40:40
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 10 ms / 5,000 ms
コード長 2,197 bytes
コンパイル時間 688 ms
コンパイル使用メモリ 117,996 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 21:29:50
合計ジャッジ時間 1,931 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 5 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 6 ms
4,384 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 9 ms
4,380 KB
testcase_09 AC 10 ms
4,380 KB
testcase_10 AC 7 ms
4,376 KB
testcase_11 AC 9 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 6 ms
4,380 KB
testcase_24 AC 8 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 9 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.conv, std.string, std.bigint;
import std.math, std.random, std.datetime;
import std.array, std.range, std.algorithm, std.container, std.format;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

class Node{
	int id;
	int staycost;
	int[int] costTo; // [相手のid: その相手までの距離]
	this(int id){
		this.id = id;
	}
	override string toString(){
		return this.id.to!string ~ "(" ~ staycost.to!string ~ ")";
	}
}

void main(){
	int n = read.to!int;
	Node[] nodes;
	foreach(i; 0 .. n) nodes ~= new Node(i);
	
	foreach(i; 0 .. n) nodes[i].staycost = read.to!int;
	debug nodes.writeln;
	
	int m = read.to!int;
	foreach(i; 0 .. m){
		int a = read.to!int, b = read.to!int, c = read.to!int;
		nodes[a].costTo[b] = c;
		nodes[b].costTo[a] = c;
	}
	debug nodes.writeln;
	
	foreach(nd; nodes) nd.costTo[nd.id] = 0;
	debug writeln;
	debug foreach(nd; nodes) writeln(nd.id, " -> ", n.iota.map!(j => (j in nd.costTo)? nd.costTo[j].to!string: "*").array.join(" "));
	
	// ワーシャル・フロイド法により求める
	foreach(k; 0 .. n){ // 経由地としてkまでを解禁した状態
		foreach(i1; 0 .. n) foreach(i2; i1 + 1 .. n){
			Node nd1 = nodes[i1], nd2 = nodes[i2];
			if(k in nd1.costTo && k in nd2.costTo){
				if(i2 !in nd1.costTo || nd1.costTo[i2] > nd1.costTo[k] + nd2.costTo[k]){
					nd1.costTo[i2] = nd1.costTo[k] + nd2.costTo[k];
					nd2.costTo[i1] = nd1.costTo[k] + nd2.costTo[k];
				}
			}
		}
		debug writeln;
		debug foreach(nd; nodes) writeln(nd.id, " -> ", n.iota.map!(j => (j in nd.costTo)? nd.costTo[j].to!string: "*").array.join(" "));
	}
	
	// 求めるものは (0 から nd1 まで移動) + (nd1 滞在) + (nd1 から nd2 まで移動) + (nd2 滞在) + (nd2 から n - 1 まで移動)
	int ans = 1000000;
	foreach(i1; 1 .. n - 1) foreach(i2; 1 .. n - 1){
		Node nd1 = nodes[i1], nd2 = nodes[i2];
		if(0 !in nd1.costTo) continue;
		if(n - 1 !in nd2.costTo) continue;
		if(i2 !in nd1.costTo) continue;
		if(i1 == i2) continue;
		int v = nd1.costTo[0] + nd1.staycost + nd1.costTo[i2] + nd2.staycost + nd2.costTo[n - 1];
		if(v < ans) ans = v;
	}
	ans.writeln;
	
}
0