Submission #50670075


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) {
    // worst Θ(N^3)解法

    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;
    foreach (e; edges) dist[e[0]][e[1]] = dist[e[1]][e[0]] = e[2];

    foreach (k; 0..N) {
        foreach (i; 0..N) foreach (j; 0..N) {
            if (dist[i][k] == long.max || dist[k][j] == long.max) continue;
            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
        }
    }

    bool ok = true;

    foreach (e; edges) {
        long d = long.max;
        foreach (i; 0..N) {
            if (i == e[0] || i == e[1]) continue;
            d = min(d, dist[e[0]][i] + dist[i][e[1]]);
        }

        if (d == e[2]) continue;
        if (d < e[2]) ok = false;

        if (e[2] < d) {
            ans += e[2];
        }
    }

    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 1332 Byte
Status AC
Exec Time 149 ms
Memory 8132 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
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 134 ms 8100 KiB
02.txt AC 137 ms 7976 KiB
03.txt AC 131 ms 8056 KiB
04.txt AC 138 ms 8020 KiB
05.txt AC 139 ms 8092 KiB
06.txt AC 136 ms 8092 KiB
07.txt AC 135 ms 8104 KiB
08.txt AC 135 ms 6940 KiB
09.txt AC 133 ms 7144 KiB
10.txt AC 136 ms 8076 KiB
11.txt AC 133 ms 8120 KiB
12.txt AC 149 ms 8132 KiB
13.txt AC 1 ms 3696 KiB
subtask0_0.txt AC 1 ms 3724 KiB
subtask0_1.txt AC 1 ms 3584 KiB
subtask0_2.txt AC 1 ms 3780 KiB
subtask0_3.txt AC 1 ms 3676 KiB