#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) {
if (abs(b[r] - b[l]) % p[l] == 0)
return true;
return false;
}
int f (int l,int r) {
return (abs(b[r] - b[l] ) / p[l]);
}
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 <= m; i ++)
d[i] = inf;
d[1] = 0;
q.push({0,1});
while (!q.empty()) {
int v = q.top().sc,cur = -q.top().fr;
q.pop();
if (cur > d[v] || d[2] <= cur)
continue;
for (int i = 1; i <= n; i ++) {
if (i == v || !check(v,i))
continue;
int len = f(v,i);
if (d[v] + len < d[i]) {
d[i] = d[v] + len;
q.push({-d[i],i});
}
}
}
if (d[2] == inf)
puts("-1");
else
cout << d[2] << endl;
}
Compilation message
skyscraper.cpp:33:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main () {
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9720 KB |
Output is correct |
2 |
Incorrect |
9 ms |
9720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
9772 KB |
Output is correct |
2 |
Incorrect |
9 ms |
9820 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9820 KB |
Output is correct |
2 |
Incorrect |
9 ms |
9948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
9948 KB |
Output is correct |
2 |
Incorrect |
9 ms |
10028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
10028 KB |
Output is correct |
2 |
Incorrect |
9 ms |
10032 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |