Submission #1694349


Source Code Expand

import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

alias P = Tuple!(int, "from", int, "to");
alias Q = Tuple!(int, "to", long, "cost");
long[][] a;
alias RBT = RedBlackTree!(P, (p, q) => a[p.from][p.to] < a[q.from][q.to], true);

void main()
{
  int n = readln.chomp.to!int;
  a = new long[][](n);
  foreach (i; 0 .. n)
  {
    a[i] = readln.chomp.split.map!(to!long).array;
  }
  RBT queue = new RBT();
  bool[][] done = new bool[][](n, n);
  foreach (i; 0 .. n)
  {
    foreach (j; i + 1 .. n)
    {
      queue.insert(P(i, j));
    }
  }
  Q[][] adj = new Q[][](n);
  long[][] dist = new long[][](n, n);
  foreach (i, ref arr; dist)
  {
    arr[] = 1L << 60;
    arr[i] = 0;
  }
  bool ng = false;
  while (queue.length)
  { // O(n^2 log n)
    P node = queue.front;
    queue.removeFront;
    long atom = dist[node.from][node.to];
    long vs = a[node.from][node.to];
    if (atom < vs)
    {
      ng = true;
      break;
    }
    if (atom == vs)
    {
      continue;
    }
    // link
    // stderr.writeln(node.from, " ", node.to, " : ", vs);
    adj[node.from] ~= Q(node.to, vs);
    adj[node.to] ~= Q(node.from, vs);
    dist[node.from][node.to] = vs;
    dist[node.to][node.from] = vs;
    foreach (i; 0 .. n)
    {
      if (dist[i][node.to] > dist[i][node.from] + dist[node.from][node.to])
      {
        dist[i][node.to] = dist[node.to][i] = dist[i][node.from] + dist[node.from][node.to];
      }
      if (dist[node.from][i] > dist[node.from][node.to] + dist[node.to][i])
      {
        dist[node.from][i] = dist[i][node.from] = dist[node.from][node.to] + dist[node.to][i];
      }
    }
    /+
    foreach (i; 0..n) {
      foreach (j; 0..n) {
        if (j) stderr.write = " ";
        stderr.write = dist[i][j] == 1L << 60 ? "*" : dist[i][j].to!string;
      }
      stderr.writeln;
    }
    stderr.writeln;
    +/
  }
  /+
  foreach (i; 0..n) {
    foreach (j; 0..n) {
      ng |= a[i][j] != dist[i][j];
    }
  }
  +/
  if (ng)
  {
    writeln = -1;
    return;
  }

  long sum = 0;
  foreach (arr; adj)
  {
    foreach (q; arr)
    {
      sum += q.cost;
    }
  }
  sum /= 2;
  
  sum.writeln;
}

Submission Info

Submission Time
Task D - Restoring Road Network
User dptbl
Language D (DMD64 v2.070.1)
Score 0
Code Size 2391 Byte
Status WA
Exec Time 54 ms
Memory 9724 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 48 ms 7676 KB
02.txt WA 49 ms 8188 KB
03.txt WA 49 ms 9724 KB
04.txt WA 54 ms 9212 KB
05.txt WA 53 ms 8316 KB
06.txt WA 47 ms 9084 KB
07.txt AC 50 ms 8700 KB
08.txt WA 49 ms 7676 KB
09.txt WA 51 ms 8572 KB
10.txt AC 51 ms 8572 KB
11.txt AC 53 ms 8444 KB
12.txt AC 43 ms 8956 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