This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#include "rail.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
int dist[5005][5005];
int get(int i, int j) {
if(dist[i][j] != 0) return dist[i][j];
return dist[i][j] = dist[j][i] = getDistance(i, j);
}
vector<int> xIsClosest[5005];
int negLoc[5005];
int vis[5005];
void findLocation(signed N, signed first, signed location[], signed stype[]) {
for(int i = 0; i < N; i++)
for(int j = 0; j < i; j++)
get(i, j);
vector<pair<int, int>> z;
for(int i = 0; i < N; i++) {
z.clear();
for(int j = 0; j < N; j++) {
if(i==j) continue;
z.push_back({get(i,j), j});
}
sort(z.begin(), z.end());
xIsClosest[z[0].second].push_back(i);
// cerr << i << "->" << z[0].second << endl;
}
z.clear();
for(int j = 1; j < N; j++) {
z.push_back({get(0,j), j});
}
sort(z.rbegin(), z.rend());
queue<pair<int, int>> q;
int xi = 1;
for(int i = 2; i < N; i++)
if(get(0,i) < get(0, xi))
xi = i;
q.push({0, 1});
while(!q.empty()) {
int i = q.front().first;
int t = q.front().second;
vis[i]=1;
stype[i] = t;
// cerr << i << ": " << t << " " << negLoc[i] << endl;
q.pop();
for(auto j: xIsClosest[i]) {
if(vis[j]) continue;
negLoc[j] = negLoc[i] + (t==1?get(i,j):-get(i,j));
q.push({j, t+(t==1)-(t==2)});
}
}
while(!z.empty()) {
int i = z.back().second;
z.pop_back();
if(vis[i]) continue;
if(get(i, 0) < get(i, xi)) {
q.push({i, 2});
negLoc[i] = get(i, 0);
} else {
q.push({i, 1});
negLoc[i] = negLoc[xi] - get(i, xi);
}
// cerr << endl;
while(!q.empty()) {
int i = q.front().first;
int t = q.front().second;
vis[i]=1;
stype[i] = t;
// cerr << i << ": " << t << " " << negLoc[i] << endl;
q.pop();
for(auto j: xIsClosest[i]) {
if(vis[j]) continue;
negLoc[j] = negLoc[i] + (t==1?get(i,j):-get(i,j));
q.push({j, t+(t==1)-(t==2)});
}
}
}
for(int i = 0; i < N; i++) {
if(i != 0 && negLoc[i] == 0) exit(-1);
location[i] = negLoc[i]+first;
}
}
# | 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... |