Submission #1691017


Source Code Expand

import java.util.*;

public class Main
{

    static Scanner sc = new Scanner(System.in);
    static int dp[][][];
    static int edges[][];
    static boolean del[][];
    public static void main(String[] args) {
        int n = sc.nextInt();

        edges = new int[n][n];
        del = new boolean[n][n];
        long total = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                edges[i][j] = sc.nextInt();
                total += edges[i][j];
            }
        }
        dp = new int[n][n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                dp[i][j][0] = edges[i][j];
            }
        }
        for (int k = 1; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    dp[i][j][k] = Math.min(dp[i][j][k-1], dp[i][k][k-1] + dp[k][j][k-1]);
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i != j && dp[i][j][n-1] != edges[i][j]){
                    System.out.println(-1);
                    return;
                }
            }
        }
        for (int k = 0; k < n; k++) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (i!=j&& j!=k && i !=k &&edges[i][j] == edges[i][k] + edges[k][j] && !del[i][j]) {
                        total -= edges[i][j];
                        del[i][j] = true;
                    }
                }
            }
        }

        total /= 2;
        System.out.println(total);
    }
}

Submission Info

Submission Time
Task D - Restoring Road Network
User vjudge2
Language Java8 (OpenJDK 1.8.0)
Score 500
Code Size 1656 Byte
Status AC
Exec Time 1001 ms
Memory 218596 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 1001 ms 207052 KB
02.txt AC 959 ms 210608 KB
03.txt AC 987 ms 212088 KB
04.txt AC 957 ms 207108 KB
05.txt AC 954 ms 212352 KB
06.txt AC 964 ms 208164 KB
07.txt AC 878 ms 211476 KB
08.txt AC 910 ms 212332 KB
09.txt AC 902 ms 218596 KB
10.txt AC 860 ms 210224 KB
11.txt AC 883 ms 212676 KB
12.txt AC 888 ms 212604 KB
13.txt AC 94 ms 21716 KB
subtask0_0.txt AC 93 ms 18900 KB
subtask0_1.txt AC 101 ms 20564 KB
subtask0_2.txt AC 96 ms 19156 KB
subtask0_3.txt AC 93 ms 21076 KB