이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "rail.h"
#include <set>
#include <cassert>
#define INF 1000000007
int Dist[5005][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] && Dist[u][i] == Dist[0][i] - Dist[0][u])
{
location[i] = location[u] - Dist[u][i];
stype[i] = 1;
}
}
}
// for (i = 0; i < N; i++)
// printf("i%d #%d %d\n", i, stype[i], location[i]);
// printf("u%d\n", u);
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;
// printf("it (%d %d) d %d -> find %d\n", it->first, it->second, d, location[v] + d);
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]);
v = it->second;
}
}
// for (i = 0; i < N; i++)
// printf("i%d #%d %d\n", i, stype[i], location[i]);
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]);
v = it->second;
}
}
for (i = 0; i < N; i++)
assert(stype[i] != 0);
// for (i = 0; i < N; i++)
// printf("i%d #%d %d\n", i, stype[i], location[i]);
}
컴파일 시 표준 에러 (stderr) 메시지
rail.cpp: In function 'void findLocation(int, int, int*, int*)':
rail.cpp:80:23: warning: 'u' may be used uninitialized in this function [-Wmaybe-uninitialized]
d = (Dist[u][v] + Dist[v][it->second] - Dist[u][it->second])/2;
~~~~~~~~~^
# | 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... |