Submission #1882939


Source Code Expand

#include <bits/stdc++.h>
#define ADD(a, b) a = (a + ll(b)) % mod
#define MUL(a, b) a = (a * ll(b)) % mod
#define MAX(a, b) a = max(a, b)
#define MIN(a, b) a = min(a, b)
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define rer(i, a, b) for(int i = int(a) - 1; i >= int(b); i--)
#define all(a) (a).begin(), (a).end()
#define sz(v) (int)(v).size()
#define pb push_back
#define sec second
#define fst first
#define debug(fmt, ...) Debug(__LINE__, ":", fmt, ##__VA_ARGS__)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<int, pi> ppi;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> mat;
typedef complex<double> comp;
void Debug() {cout << '\n'; }
template<class FIRST, class... REST>void Debug(FIRST arg, REST... rest){
	cout<<arg<<" ";Debug(rest...);}
template<class T>ostream& operator<<(ostream& out,const vector<T>& v) {
	out<<"[";if(!v.empty()){rep(i,0,sz(v)-1)out<<v[i]<<", ";out<<v.back();}out<<"]";return out;}
template<class S, class T>ostream& operator<<(ostream& out,const pair<S, T>& v){
	out<<"("<<v.first<<", "<<v.second<<")";return out;}
const int MAX_N = 200010;
const int MAX_V = 100010;
const double eps = 1e-6;
const ll mod = 1000000007;
const int inf = 1 << 29;
const ll linf = 1LL << 60;
const double PI = 3.14159265358979323846;
///////////////////////////////////////////////////////////////////////////////////////////////////

int N;
ll A[310][310];
ll B[310][310];

void solve() {
	cin >> N;
	rep(i, 0, N) {
		rep(j, 0, N) {
			cin >> A[i][j];
			B[i][j] = A[i][j];
		}
	}
	rep(i, 0, N) {
		rep(j, 0, N) {
			rep(k, 0, N) {
				MIN(B[i][j], B[i][k] + B[k][j]);
			}
		}
	}
	rep(i, 0, N) {
		rep(j, 0, N) {
			if(A[i][j] != B[i][j]) {
				cout << "-1\n";
				return;
			}
		}
	}
	ll ans = 0;
	rep(i, 0, N) {
		rep(j, 0, N) {
			if(i == j) continue;
			bool found = false;
			rep(k, 0, N) {
				if(i == k || j == k) continue;
				if(A[i][j] == B[i][k] + B[k][j]) {
					found = true; break;
				}
			}
			if(!found) ans += A[i][j];
		}
	}
	cout << ans / 2 << "\n";
}

int main() {
#ifndef LOCAL
	ios::sync_with_stdio(false);
    cin.tie(0);
#endif
    cout << fixed;
	cout.precision(20);
#ifdef LOCAL
    freopen("in.txt", "rt", stdin);
#endif	
	solve();
#ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

Submission Info

Submission Time
Task D - Restoring Road Network
User omochana2
Language C++14 (GCC 5.4.1)
Score 500
Code Size 2507 Byte
Status AC
Exec Time 83 ms
Memory 1664 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 56 ms 1664 KB
02.txt AC 56 ms 1664 KB
03.txt AC 58 ms 1664 KB
04.txt AC 59 ms 1664 KB
05.txt AC 62 ms 1664 KB
06.txt AC 83 ms 1664 KB
07.txt AC 52 ms 1664 KB
08.txt AC 62 ms 1664 KB
09.txt AC 63 ms 1664 KB
10.txt AC 52 ms 1664 KB
11.txt AC 52 ms 1664 KB
12.txt AC 53 ms 1664 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