This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
#define pii array<int,2>
vector<vector<int>> dis;
map<pii,int> mp;
int query(int a, int b) {
if (mp.find({a,b}) != mp.end()) {
return mp[{a,b}];
}
if (mp.find({b,a}) != mp.end()) {
return mp[{b,a}];
}
int d = getDistance(a, b);
mp[{a,b}] = d;
return d;
}
int hubDistance(int N, int sub) {
dis.assign(N, vector<int>(N, 1e9));
for (int i=0;i<N;i++) {
dis[i][i] = 0;
}
for (int i=0;i<N;i++) {
for (int j=i+1;j<N;j++) {
dis[i][j] = dis[j][i] = query(i, j);
}
}
int retVal = 0;
pii foundPos = {-1, -1};
// find maximum value and respective nodes
for (int i=0;i<N;i++) {
for (int j=i+1;j<N;j++) {
if (dis[i][j] > retVal) {
retVal = dis[i][j];
foundPos = {i, j};
}
}
}
int mxDis = retVal;
for (int i=0;i<N;i++) {
if (i == foundPos[0] || i == foundPos[1]) continue;
int q1 = query(foundPos[0], i);
int q2 = query(foundPos[1], i);
int sum = q1 + q2;
int weightTo = (sum - retVal) / 2;
mxDis = min(mxDis, max(q1 - weightTo, q2 - weightTo));
}
return mxDis;
}
Compilation message (stderr)
towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:24:28: warning: unused parameter 'sub' [-Wunused-parameter]
24 | int hubDistance(int N, int sub) {
| ~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |