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 <bits/stdc++.h>
#define fr first
#define sc second
#define OK puts("OK");
#define pb push_back
#define mk make_pair
#define int long long
using namespace std;
typedef long long ll;
const int inf = (int)1e15 + 7;
const int N = (int)2e5 + 7;
int n,m;
int b[N],p[N];
int d[N];
vector <int> vec[N];
vector <pair<int,int> > g[N];
priority_queue<pair<int,int> > q;
bool check (int l,int r,int x) {
if (abs(r - l) % x == 0)
return true;
return false;
}
int f (int l,int r,int x) {
return (abs(r - l) / x);
}
main () {
cin >> n >> m;
for (int i = 1; i <= m; i ++) {
cin >> b[i] >> p[i];
b[i] ++;
vec[b[i]].pb(p[i]);
}
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++) {
int mnn = inf;
if (i == j)
continue;
for (auto to : vec[i]) {
if (check(i,j,to))
mnn = min(mnn,f(i,j,to));
}
if (mnn != inf)
g[i].pb(mk(j,mnn));
}
}
for (int i = 1; i <= n; i ++)
d[i] = inf;
d[b[1]] = 0;
q.push({0,b[1]});
while (!q.empty()) {
int v = q.top().sc,cur = -q.top().fr;
q.pop();
if (cur > d[v])
continue;
if (b[2] == v) {
cout << d[v] << endl;
return 0;
}
for (auto to : g[v]) {
if (d[v] + to.sc < d[to.fr]) {
d[to.fr] = d[v] + to.sc;
q.push({-d[to.fr],to.fr});
}
}
}
if (d[b[2]] == inf)
puts("-1");
else
cout << d[b[2]] << endl;
}
Compilation message (stderr)
skyscraper.cpp:33:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
# | 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... |