Submission #1695979


Source Code Expand

import java.util.*;

public class Main {
//    private static final String ex = "5\n" +
//            "0 21 18 11 28\n" +
//            "21 0 13 10 26\n" +
//            "18 13 0 23 13\n" +
//            "11 10 23 0 17\n" +
//            "28 26 13 17 0";
//    private static final String ex = "3\n" +
//        "0 1 3\n" +
//        "1 0 2\n" +
//        "3 2 0";
    private static final String ex = "3\n" +
        "0 1 3\n" +
        "1 0 1\n" +
        "3 1 0";
//    private static final String ex = "3\n" +
//        "0 1000000000 1000000000\n" +
//        "1000000000 0 1000000000\n" +
//        "1000000000 1000000000 0";

    public static void main(String[] args) {
        System.out.println(solve(new Scanner(System.in)));
//        System.out.println(solve(new Scanner(ex)));
    }

    private static long solve(Scanner scanner) {
        int N = Integer.parseInt(scanner.nextLine());

        long[][] graph = new long[N][N];
        for (int i = 0; i < N; i ++) {
            String[] split = scanner.nextLine().split(" ");
            for (int j = 0; j < N; j ++) {
                graph[i][j] = Long.parseLong(split[j]);
            }
        }
        
        for (int i = 0; i < N; i ++) {
            for (int j = 0; j < N; j ++) {
                if (i == 0 && j == 0 && graph[i][j] != 0) return -1;
                if (graph[i][j] != graph[j][i]) return -1;
            }
        }

        long result = 0;
        for (int i = 0; i < N; i ++) {
            for (int j = 0; j < N; j ++) {
                Set<Long> set = new HashSet<>();
                long minI = Long.MAX_VALUE;
                for (int k = 0; k < N; k ++) {
                    if (i == k) continue;

                    set.add(graph[i][k]);
                    minI = Math.min(minI, graph[i][k]);
                }
                long minJ = Long.MAX_VALUE;
                boolean flag = false;
                for (int k = 0; k < N; k ++) {
                    if (j == k) continue;

                    if (set.contains(graph[i][j] - graph[k][j])) {
                        flag = true;
                    }
                    minJ = Math.min(minJ, graph[k][j]);
                }
                if (minI + minJ > graph[i][j]) {
                    result += graph[i][j];
                } else if (!flag) return -1;
            }
        }

        return result / 2;
    }
}

Submission Info

Submission Time
Task D - Restoring Road Network
User g_votte
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 2458 Byte
Status WA
Exec Time 450 ms
Memory 62224 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
AC × 4
AC × 9
WA × 8
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 WA 338 ms 43576 KB
02.txt WA 288 ms 46464 KB
03.txt WA 450 ms 62224 KB
04.txt WA 420 ms 56732 KB
05.txt WA 295 ms 43832 KB
06.txt WA 333 ms 42828 KB
07.txt AC 345 ms 44828 KB
08.txt WA 237 ms 44884 KB
09.txt WA 252 ms 39304 KB
10.txt AC 318 ms 44876 KB
11.txt AC 310 ms 40616 KB
12.txt AC 275 ms 43344 KB
13.txt AC 96 ms 19796 KB
subtask0_0.txt AC 96 ms 19796 KB
subtask0_1.txt AC 98 ms 19668 KB
subtask0_2.txt AC 98 ms 21716 KB
subtask0_3.txt AC 99 ms 21844 KB