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 "rail.h"
#include <map>
#include <set>
#define INF 1000000007
int Dist[5005][5005];
int leftU[5005];
bool operator<(std::pair<int, int> a, std::pair<int, int> b)
{
return (a.first < b.first || (a.first == b.first && a.second < b.second));
}
void findLocation(int N, int first, int location[], int stype[])
{
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (i == j)
Dist[i][j] = 0;
else
Dist[i][j] = -1;
}
}
location[0] = first;
stype[0] = 1;
int u, ud = INF;
for (i = 0; i < N; i++)
{
if (Dist[0][i] == -1)
{
Dist[0][i] = getDistance(0, i);
Dist[i][0] = Dist[0][i];
if (Dist[0][i] < ud)
{
u = i;
ud = Dist[0][i];
}
}
}
if (ud == INF)
return;
location[u] = first + ud;
stype[u] = 2;
for (i = 0; i < N; i++)
{
if (Dist[u][i] == -1)
{
Dist[u][i] = getDistance(u, i);
Dist[i][u] = Dist[u][i];
if (Dist[u][i] < Dist[u][0])
{
location[i] = location[u] - Dist[u][i];
stype[i] = 1;
}
}
}
int v, d;
std::set<std::pair<int, int> > M;
std::set<int> C;
M.clear();
C.clear();
v = 0;
C.insert(location[0]);
for (i = 0; i < N; i++)
{
if (stype[i] != 0) continue;
if (Dist[0][i] != Dist[0][u] + Dist[u][i]) continue;
M.insert({Dist[u][i], i});
}
std::set<std::pair<int, int> >::iterator it;
for (it = M.begin(); it != M.end(); it++)
{
if (Dist[v][it->second] == -1)
{
Dist[v][it->second] = getDistance(v, it->second);
Dist[it->second][v] = Dist[v][it->second];
}
d = (Dist[u][v] + Dist[v][it->second] - Dist[u][it->second])/2;
if (C.find(location[v] + d) != C.end())
{
location[it->second] = location[v] + Dist[v][it->second];
stype[it->second] = 2;
}
else
{
location[it->second] = location[u] - Dist[u][it->second];
stype[it->second] = 1;
C.insert(location[it->second]);
}
}
M.clear();
C.clear();
v = u;
C.insert(location[u]);
for (i = 0; i < N; i++)
{
if (stype[i] != 0) continue;
M.insert({Dist[0][i], i});
}
for (it = M.begin(); it != M.end(); it++)
{
if (Dist[v][it->second] == -1)
{
Dist[v][it->second] = getDistance(v, it->second);
Dist[it->second][v] = Dist[v][it->second];
}
d = (Dist[0][v] + Dist[v][it->second] - Dist[0][it->second])/2;
if (C.find(location[v] - d) != C.end())
{
location[it->second] = location[v] - Dist[v][it->second];
stype[it->second] = 1;
}
else
{
location[it->second] = location[0] + Dist[0][it->second];
stype[it->second] = 2;
C.insert(location[it->second]);
}
}
}
Compilation message (stderr)
rail.cpp: In function 'void findLocation(int, int, int*, int*)':
rail.cpp:104:33: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
Dist[v][it->second] = getDistance(v, it->second);
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |