Submission #1694364


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();
  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;
    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];
      }
    }
    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 500
Code Size 2771 Byte
Status AC
Exec Time 117 ms
Memory 9340 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 114 ms 9340 KB
02.txt AC 114 ms 7420 KB
03.txt AC 116 ms 8316 KB
04.txt AC 111 ms 8828 KB
05.txt AC 113 ms 8700 KB
06.txt AC 114 ms 7420 KB
07.txt AC 110 ms 9212 KB
08.txt AC 108 ms 7292 KB
09.txt AC 109 ms 7932 KB
10.txt AC 117 ms 8572 KB
11.txt AC 111 ms 9340 KB
12.txt AC 42 ms 8316 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