結果

問題 No.17 2つの地点に泊まりたい
ユーザー iicafiaxusiicafiaxus
提出日時 2018-12-09 17:58:57
言語 D
(dmd 2.105.2)
結果
WA  
実行時間 -
コード長 2,176 bytes
コンパイル時間 713 ms
コンパイル使用メモリ 120,288 KB
実行使用メモリ 4,568 KB
最終ジャッジ日時 2023-09-03 21:29:19
合計ジャッジ時間 4,796 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

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; 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; 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) foreach(i2; i1 + 1 .. n - 1){
		Node nd1 = nodes[i1], nd2 = nodes[i2];
		int v1 = nd1.costTo[0] + nd1.staycost + nd1.costTo[i2] + nd2.staycost + nd2.costTo[n - 1];
		int v2 = nd2.costTo[0] + nd2.staycost + nd2.costTo[i1] + nd1.staycost + nd1.costTo[n - 1];
		if(v1 < ans) ans = v1;
		if(v2 < ans) ans = v2;
	}
	ans.writeln;
	
}
0