結果
| 問題 |
No.17 2つの地点に泊まりたい
|
| コンテスト | |
| ユーザー |
yosupot
|
| 提出日時 | 2017-01-01 22:14:29 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,152 bytes |
| コンパイル時間 | 892 ms |
| コンパイル使用メモリ | 138,332 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-06-12 06:13:06 |
| 合計ジャッジ時間 | 1,812 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 WA * 6 |
ソースコード
/+ 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^^9).dist;
}
int ans = 10^^9;
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;
}
yosupot