結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
iicafiaxus
|
| 提出日時 | 2018-12-09 18:40:40 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 9 ms / 5,000 ms |
| コード長 | 2,197 bytes |
| コンパイル時間 | 706 ms |
| コンパイル使用メモリ | 126,444 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-13 02:08:47 |
| 合計ジャッジ時間 | 1,570 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 27 |
ソースコード
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;
}
iicafiaxus