Submission #1963963


Source Code Expand

Copy
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int i,j,k;
  int n;
  cin>>n;
  vector<vi> a(n,vi(n)),b(n,vi(n,1));
  rep(i,n)rep(j,n)cin>>a[i][j];
  rep(k,n)rep(i,n)rep(j,n)if(k!=i && k!=j)
    if(a[i][j]>a[i][k]+a[k][j]){
      cout<<-1<<endl;
      return 0;
    }else if(a[i][j]==a[i][k]+a[k][j]){
      b[i][j]=0;
    }
  ll ans=0;
  rep(i,n)rep(j,i)if(b[i][j])ans+=a[i][j];
  cout<<ans<<endl;
}

Submission Info

Submission Time
Task D - Restoring Road Network
User rika0384
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1008 Byte
Status
Exec Time 91 ms
Memory 1024 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask0_0.txt, subtask0_1.txt, subtask0_2.txt, subtask0_3.txt
All 500 / 500 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 83 ms 1024 KB
02.txt 86 ms 1024 KB
03.txt 91 ms 1024 KB
04.txt 84 ms 1024 KB
05.txt 85 ms 1024 KB
06.txt 82 ms 1024 KB
07.txt 32 ms 1024 KB
08.txt 77 ms 1024 KB
09.txt 78 ms 1024 KB
10.txt 32 ms 1024 KB
11.txt 32 ms 1024 KB
12.txt 37 ms 1024 KB
13.txt 1 ms 256 KB
subtask0_0.txt 1 ms 256 KB
subtask0_1.txt 1 ms 256 KB
subtask0_2.txt 1 ms 256 KB
subtask0_3.txt 1 ms 256 KB