Submission #1604142


Source Code Expand

Copy
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/






typedef long long ll;
int N, A[303][303];
ll d[303][303];
#define INF INT_MAX/2
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N) rep(j, 0, N) cin >> A[i][j];

    vector<pair<ll, pair<int, int>>> v;
    rep(i, 0, N) rep(j, i + 1, N) v.push_back({ -A[i][j],{ i,j } });
    sort(v.begin(), v.end());

    ll ans = 0;
    rep(i, 0, N) rep(j, i + 1, N) ans += A[i][j];
    fore(p, v) {
        ll c = -p.first;
        int a = p.second.first;
        int b = p.second.second;
        if (A[a][b] == INF) continue;

        rep(i, 0, N) if (i != a && i != b && A[a][i] != INF && A[i][b] != INF) {
            if (A[a][i] + A[i][b] == c) {
                ans -= c;
                A[a][b] = A[b][a] = INF;
                break;
            } else if (A[a][i] + A[i][b] < c) {
                printf("-1\n");
                return;
            }
        }
    }
    cout << ans << endl;
}

Submission Info

Submission Time
Task D - Restoring Road Network
User hamayanhamayan
Language C++14 (GCC 5.4.1)
Score 500
Code Size 1971 Byte
Status AC
Exec Time 52 ms
Memory 1784 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 16 ms 1784 KB
02.txt AC 16 ms 1784 KB
03.txt AC 19 ms 1784 KB
04.txt AC 22 ms 1784 KB
05.txt AC 26 ms 1784 KB
06.txt AC 52 ms 1784 KB
07.txt AC 13 ms 1784 KB
08.txt AC 26 ms 1784 KB
09.txt AC 28 ms 1784 KB
10.txt AC 13 ms 1784 KB
11.txt AC 13 ms 1784 KB
12.txt AC 14 ms 1784 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