#include<bits/stdc++.h>
#include "rail.h"
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
void findLocation(int N, int first, int location[], int stype[]){
location[0]=first;
stype[0]=1;
if(N==1){
return;
}
int dist[N][N];
for(int i=0;i<N;i++){
dist[i][i]=0;
for(int j=i+1;j<N;j++){
dist[i][j]=getDistance(i,j);
dist[j][i]=dist[i][j];
}
}
vector<int>g[N];
vector<pair<int,int>>v;
for(int i=0;i<N;i++){
int mn=1e9,idx=-1;
for(int j=0;j<N;j++){
if(j==i) continue;
if(dist[i][j]<mn){
mn=dist[i][j];
idx=j;
}
}
g[i].pb(idx);
g[idx].pb(i);
v.pb({i,idx});
//cout<<"PAIR "<<i<<' '<<idx<<endl;
}
int D=-1;
for(auto p:v){
int x=p.ff,y=p.ss;
if(x==0||y==0){
if(x==0){
D=y;
}
if(x==0) swap(x,y);
location[x]=first+dist[x][y];
stype[x]=2;
}
}
vector<bool>used(N,false);
queue<int>q;
q.push(0);
used[0]=true;
while(!q.empty()){
int x=q.front();
q.pop();
//cout<<"FOUND "<<x<<' '<<stype[x]<<endl;
for(int y:g[x]){
if(!used[y]){
used[y]=true;
if(stype[x]==1){
location[y]=location[x]+dist[x][y];
stype[y]=2;
}
else{
location[y]=location[x]-dist[x][y];
stype[y]=1;
}
q.push(y);
}
}
}
vector<array<int,3>>V;
for(int i=0;i<v.size();i++){
if(used[v[i].ff]||used[v[i].ss]){
continue;
}
V.pb({dist[v[i].ff][v[i].ss],v[i].ff,v[i].ss});
}
sort(all(V));
for(auto p:V){
int x=p[1],y=p[2];
if(used[x]){
stype[y]=3-stype[x];
if(stype[x]==1){
location[y]=location[x]+dist[x][y];
}
else{
location[y]=location[x]-dist[x][y];
}
continue;
}
if(used[y]){
stype[x]=3-stype[y];
if(stype[y]==1){
location[x]=location[y]+dist[x][y];
}
else{
location[x]=location[y]-dist[x][y];
}
continue;
}
used[x]=true;
used[y]=true;
if(dist[x][0]==dist[x][D]+dist[D][0]){//Left
if(dist[x][D]>dist[y][D]){
swap(x,y);//x then y
}
location[x]=location[D]-dist[D][x];
location[y]=location[x]+dist[x][y];
stype[x]=1;
stype[y]=2;
}
else{//Right
if(dist[x][0]>dist[y][0]){
swap(x,y);//y then x
}
location[x]=location[0]+dist[0][x];
location[y]=location[x]-dist[x][y];
stype[x]=2;
stype[y]=1;
}
}
}
# | 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... |