Submission #1695309


Source Code Expand

import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;


void main() {
    int N;
    int[][] A;

    scan(N);
    A = new int[][](N, N);
    iota(N).each!(i => A[i] = readln.split.to!(int[]));

    long ans;

    foreach (i ; 0 .. N) {
        foreach (j ; i + 1 .. N) {
            bool flag = true;

            foreach (k ; 0 .. N) {
                if (k == i || k == j) continue;
                if (A[i][j] > A[i][k] + A[k][j]) {
                    writeln(-1);
                    return;
                }
                if (A[i][j] == A[i][k] + A[k][j]) {
                    flag = false;
                    break;
                }
            }

            if (flag) ans += A[i][j];
        }
    }

    writeln(ans);
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
} 

Submission Info

Submission Time
Task D - Restoring Road Network
User nanae
Language D (DMD64 v2.070.1)
Score 500
Code Size 1281 Byte
Status AC
Exec Time 63 ms
Memory 8060 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 4
AC × 17
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 26 ms 7164 KB
02.txt AC 26 ms 7420 KB
03.txt AC 27 ms 5372 KB
04.txt AC 30 ms 7292 KB
05.txt AC 34 ms 6780 KB
06.txt AC 63 ms 7164 KB
07.txt AC 21 ms 7932 KB
08.txt AC 30 ms 7420 KB
09.txt AC 30 ms 7036 KB
10.txt AC 21 ms 8060 KB
11.txt AC 20 ms 5500 KB
12.txt AC 23 ms 7676 KB
13.txt AC 1 ms 256 KB
subtask0_0.txt AC 1 ms 256 KB
subtask0_1.txt AC 1 ms 256 KB
subtask0_2.txt AC 1 ms 256 KB
subtask0_3.txt AC 1 ms 256 KB