이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
const int LG = 21;
const int N = 400005;
const long long MOD = 1e9 + 7;
const long long INF = 1e9;
const long long INFLL = 1e18;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define fi first
#define se second
#define re return
#define pb push_back
#define uniq(x) sort(all(x)); (x).resize(unique(all(x)) - (x).begin())
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " " << #x << " " << x << endl
#define ln cerr << __LINE__ << endl
#else
#define dbg(x) void(0)
#define ln void(0)
#endif // LOCAL
const int K = 200;
signed main()
{
int n, m;
cin >> n >> m;
vector<map<int, int> > dist_big(n);
vector<vector<int> > dist_small(n, vector<int>(K, INF));
vector<int> b(m), p(m);
vector<vi> kek(n);
for (int i = 0; i < m; i++) cin >> b[i] >> p[i], kek[b[i]].push_back(p[i]);
for (int i = 0; i < n; i++)
{
uniq(kek[i]);
}
deque<pii> q;
int s = b[0], f = b[1];
for (auto i : kek[s])
{
if (i < K) dist_small[s][i] = 0;
else dist_big[s][i] = 0;
q.push_back({s, i});
}
vector<int> init(n, INF);
init[s] = 0;
if (f == s) cout << 0, exit(0);
while (q.size())
{
pair<int, int> x = q.front();
q.pop_front();
int di = 0;
if (x.se < K) di = dist_small[x.fi][x.se];
else di = dist_big[x.fi][x.se];
if (init[x.first] == INF)
{
for (auto j : kek[x.fi])
{
if (j < K)
{
if (dist_small[x.fi][j] == INF)
{
dist_small[x.fi][j] = di;
q.push_front({x.fi, j});
}
}
else
{
if (dist_big[x.fi].find(j) == dist_big[x.fi].end())
{
dist_big[x.fi][j] = di;
q.push_front({x.fi, j});
}
}
}
init[x.fi] = di;
if (x.fi == f) cout << init[x.fi], exit(0);
}
if (x.se < K)
{
if (x.fi - x.se >= 0 && dist_small[x.fi - x.se][x.se] == INF)
{
dist_small[x.fi - x.se][x.se] = di + 1;
q.push_back({x.fi - x.se, x.se});
}
if (x.fi + x.se < n && dist_small[x.fi + x.se][x.se] == INF)
{
dist_small[x.fi + x.se][x.se] = di + 1;
q.push_back({x.fi + x.se, x.se});
}
}
else
{
if (x.fi - x.se >= 0 && dist_big[x.fi - x.se].find(x.se) == dist_big[x.fi - x.se].end())
{
dist_big[x.fi - x.se][x.se] = di + 1;
q.push_back({x.fi - x.se, x.se});
}
if (x.fi + x.se < n && dist_big[x.fi + x.se].find(x.se) == dist_big[x.fi + x.se].end())
{
dist_big[x.fi + x.se][x.se] = di + 1;
q.push_back({x.fi + x.se, x.se});
}
}
}
cout << -1;
}
/* Note:
Check constants at the beginning of the code.
N is set to 4e5 but be careful in problems with large constant factor.
Setting N in every problem is more effective.
Check corner cases.
N = 1
No def int long long for now.
Add something here.
*/
# | 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... |