이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 30000+5;
const int M = 7e6 + 1;
int mod = 998244353;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 31;
int mult(int a, int b) {
return a * 1LL * b % mod;
}
int sum(int a, int b) {
if (a + b < 0)
return a + b + mod;
if (a + b >= mod)
return a + b - mod;
return a + b;
}
ll binpow(ll a, ll n) {
if (n == 0)
return 1;
if (n % 2 == 1) {
return binpow(a, n - 1) * a % mod;
} else {
ll b = binpow(a, n / 2);
return b * b % mod;
}
}
int n, m, x[N], p[N], d[N];
map<pii, vector<int>> ps;
vector<pii> g[N];
void solve(){
cin >> n >> m;
for(int i = 0; i < m; i++){
cin >> x[i] >> p[i];
ps[{x[i] % p[i], p[i]}].pb(x[i]);
}
for(auto e : ps){
int st = e.F.F, d = e.F.S;
vector<int> xs = e.S;
sort(all(xs));
xs.resize(unique(all(xs)) - xs.begin());
for(int i = 0; i < sz(xs) - 1; i++){
int l = xs[i], r = xs[i + 1];
for(int j = l; j <= r; j += d){
g[l].pb({j, (j - l) / d});
g[r].pb({j, (r - j) / d});
}
}
for(int j = st; j <= xs[0]; j += d){
g[xs[0]].pb({j, (xs[0] - j) / d});
}
for(int j = xs[sz(xs) - 1]; j < n; j += d){
g[xs[sz(xs) - 1]].pb({j, (j - xs[sz(xs) - 1]) / d});
}
}
for(int i = 0; i < n; i++)
d[i] = infi;
d[x[0]] = 0;
set<pii> st;
st.insert({0, x[0]});
while(!st.empty()){
int v = st.begin()->S;
st.erase(st.begin());
for(auto e : g[v]){
int u = e.F, w = e.S;
if(d[u] > d[v] + w){
st.erase({d[u], u});
d[u] = d[v] + w;
st.insert({d[u], u});
}
}
}
cout << (d[x[1]] == infi ? -1 : d[x[1]]) << '\n';
}
signed main() {
mispertion;
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# | 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... |