/+ dub.sdl: name "A" dependency "dcomp" version=">=0.2.0" +/ import std.stdio, std.datetime, std.range, std.algorithm, std.conv; import std.numeric; import std.typecons; // import dcomp.scanner; // import dcomp.graph.dijkstra; alias Edge = Tuple!(int, "to", int, "dist"); int main(string[] argv) { auto sc = new Scanner(); int n; sc.read(n); Edge[][] g = new Edge[][](n); int[] s = new int[n]; foreach (i; 0..n) { sc.read(s[i]); } int m; sc.read(m); foreach (i; 0..m) { int a, b, c; sc.read(a, b, c); g[a] ~= Edge(b, c); g[b] ~= Edge(a, c); } int[][] dist = new int[][](n); foreach (i; 0..n) { dist[i] = g.dijkstra!int(i, 10^^8).dist; } int ans = 10^^8; foreach (i; 1..n-1) { foreach (j; 1..n-1) { if (i == j) continue; auto di = 0; di += dist[0][i] + dist[i][j] + dist[j][n-1]; di += s[i] + s[j]; ans = min(ans, di); } } writeln(ans); return 0; } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/scanner.d */ // module dcomp.scanner; class Scanner { import std.stdio : File, stdin; import std.conv : to; import std.range : front, popFront, array, ElementType; import std.array : split; import std.traits : isSomeChar, isStaticArray, isArray; import std.algorithm : map; File f; this(File f = stdin) { this.f = f; } string[] buf; private bool succ() { while (!buf.length) { if (f.eof) return false; buf = f.readln.split; } return true; } private bool readSingle(T)(ref T x) { if (!succ()) return false; static if (isArray!T) { alias E = ElementType!T; static if (isSomeChar!E) { //string or char[10] etc x = buf.front; buf.popFront; } else { static if (isStaticArray!T) { //static assert(buf.length == T.length); } x = buf.map!(to!E).array; buf.length = 0; } } else { x = buf.front.to!T; buf.popFront; } return true; } int read(T, Args...)(ref T x, auto ref Args args) { if (!readSingle(x)) return 0; static if (args.length == 0) { return 1; } else { return 1 + read(args); } } } unittest { import std.path : buildPath; import std.file : tempDir; import std.algorithm : equal; import std.stdio : File; string fileName = buildPath(tempDir, "kyuridenanmaida.txt"); auto fout = File(fileName, "w"); fout.writeln("1 2 3"); fout.writeln("ab cde"); fout.writeln("1.0 1.0 2.0"); fout.close; Scanner sc = new Scanner(File(fileName, "r")); int a; int[2] b; char[2] c; string d; double e; double[] f; sc.read(a, b, c, d, e, f); assert(a == 1); assert(equal(b[], [2, 3])); assert(equal(c[], "ab")); assert(equal(d, "cde")); assert(e == 1.0); assert(equal(f, [1.0, 2.0])); } /* IMPORT /Users/yosupo/Program/dcomp/source/dcomp/graph/dijkstra.d */ // module dcomp.graph.djikstra; struct Dijkstra(T) { T[] dist; } Dijkstra!D dijkstra(D, T)(T g, int s, D inf = D.max) { import std.typecons : Tuple; import std.container.array; import std.container.binaryheap; import std.container.util : make; size_t V = g.length; Dijkstra!D dijk; dijk.dist.length = V; dijk.dist[] = inf; alias P = Tuple!(int, "to", D, "dist"); auto q = heapify!"a.dist>b.dist"(make!(Array!P)([P(D(0), s)])); dijk.dist[s] = D(0); while (!q.empty) { P p = q.front; q.popFront(); if (dijk.dist[p[1]] < p[0]) continue; foreach (e; g[p[1]]) { if (p[0]+e.dist < dijk.dist[e.to]) { dijk.dist[e.to] = p[0] + e.dist; q.insert(P(dijk.dist[e.to], e.to)); } } } return dijk; }