Submission #50664969
Source Code Expand
import std;
void main () {
int N = readln.chomp.to!int;
auto A = new int[][](N, 0);
foreach (i; 0..N) A[i] = readln.split.to!(int[]);
solve(N, A);
}
void solve (int N, int[][] A) {
// 非負の辺しかないので、Aの最小の値に関しては確定できる。
// それ以外にも、重みの昇順に見ていくことを考えると、
// 1. 重み分の辺をはる
// 2. すでに制約が満たされている
// のどちらかしかありえない。なぜなら、重みより小さい辺を新しくはると別の場所で制約違反が必ず起きるし、より大きな辺をはるとどうやっても達成できないから。
// さらに、追加していく辺が重み昇順であることを考えると、タイプ1の時はチェックすらしなくてよい。
Tuple!(int, int, int)[] edges;
foreach (i; 0..N) {
foreach (j; i+1..N) {
edges ~= tuple(i, j, A[i][j]);
}
}
edges.sort!"a[2] < b[2]";
long ans = 0;
auto dist = new long[][](N, N);
foreach (d; dist) d[] = long.max;
foreach (i; 0..N) dist[i][i] = 0;
bool ok = true;
foreach (e; edges) {
if (dist[e[0]][e[1]] == e[2]) continue;
if (dist[e[0]][e[1]] < e[2]) ok = false;
if (e[2] < dist[e[0]][e[1]]) {
ans += e[2];
// floyd-warshallっぽく更新
// 辺をセット
dist[e[0]][e[1]] = dist[e[1]][e[0]] = e[2];
foreach (i; 0..N) foreach (j; 0..N) {
if (dist[i][e[0]] < long.max && dist[e[1]][j] < long.max) {
dist[i][j] = dist[j][i] = min(dist[i][j], dist[i][e[0]] + dist[e[0]][e[1]] + dist[e[1]][j]);
}
}
}
}
if (ok) {
writeln(ans);
}
else {
writeln(-1);
}
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - Restoring Road Network |
| User | InTheBloom |
| Language | D (DMD 2.104.0) |
| Score | 500 |
| Code Size | 1924 Byte |
| Status | AC |
| Exec Time | 517 ms |
| Memory | 8288 KiB |
Compile Error
/home/runner/.dub/packages/mir-algorithm/3.20.4/mir-algorithm/source/mir/serde.d(52,9): Deprecation: alias this for classes/interfaces is deprecated
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 500 / 500 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt |
| All | 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 01.txt | AC | 51 ms | 8160 KiB |
| 02.txt | AC | 52 ms | 8144 KiB |
| 03.txt | AC | 60 ms | 8288 KiB |
| 04.txt | AC | 65 ms | 8072 KiB |
| 05.txt | AC | 66 ms | 8036 KiB |
| 06.txt | AC | 102 ms | 8144 KiB |
| 07.txt | AC | 117 ms | 8140 KiB |
| 08.txt | AC | 497 ms | 6916 KiB |
| 09.txt | AC | 517 ms | 7084 KiB |
| 10.txt | AC | 120 ms | 8196 KiB |
| 11.txt | AC | 182 ms | 8152 KiB |
| 12.txt | AC | 497 ms | 8136 KiB |
| 13.txt | AC | 1 ms | 3820 KiB |
| subtask0_0.txt | AC | 1 ms | 3720 KiB |
| subtask0_1.txt | AC | 1 ms | 3728 KiB |
| subtask0_2.txt | AC | 1 ms | 3756 KiB |
| subtask0_3.txt | AC | 1 ms | 3672 KiB |