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) 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; }