#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define endl "\n"
#define pii pair<ll,ll>
#define pb push_back
#define vi vector<ll>
#define pque priority_queue
#define pqueg priority_queue<ll,vector<ll>,greater<ll>>
#define que queue<ll>
#define FOR(m,i,n) for(int i=(m); i<=(n); i++)
#define FORM(m,i,n) for(int i=(m); i>=(n); i--)
ll n,m;
pii a[30300];
vector<pii> adj[30300];
priority_queue<pii,vector<pii>,greater<pii>> pq;
ll dist[30030];
bool vis[30030];
ll temp1,temp2;
ll temp;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
FOR(1,i,m) {
cin >> a[i].fi >> a[i].se;
if(i == 1) {
temp1 = a[i].fi;
}
else if(i == 2) {
temp2 = a[i].fi;
}
}
sort(a+1,a+m+1);
FOR(1,i,m) {
FOR(1,j,m) {
if(a[i].fi == a[j].fi) continue;
if(abs(a[i].fi - a[j].fi) % a[i].se == 0) {
adj[a[i].fi].pb({a[j].fi,abs(a[i].fi - a[j].fi) / a[i].se});
}
}
}
pq.push({0,temp1});
FOR(0,i,n) {
dist[i] = 1e9;
}
dist[temp1] = 0;
while(!pq.empty()) {
pii x = pq.top();
pq.pop();
for(auto i : adj[x.se]) {
if(dist[i.fi] > dist[x.se] + i.se) {
dist[i.fi] = dist[x.se] + i.se;
pq.push({dist[i.fi],i.fi});
}
}
}
if(dist[temp2] == 1e9) {
cout << -1 << endl;
}
else {
cout << dist[temp2] << endl;
}
}
/*
5 3
0 2
1 1
4 1
5 3
0 2
1 3
2 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
1040 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
20 ms |
4116 KB |
Output is correct |
12 |
Correct |
4 ms |
1744 KB |
Output is correct |
13 |
Correct |
10 ms |
8388 KB |
Output is correct |
14 |
Correct |
19 ms |
1756 KB |
Output is correct |
15 |
Correct |
19 ms |
1752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
1040 KB |
Output is correct |
8 |
Correct |
1 ms |
1036 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
22 ms |
4076 KB |
Output is correct |
12 |
Correct |
5 ms |
1744 KB |
Output is correct |
13 |
Correct |
11 ms |
8516 KB |
Output is correct |
14 |
Correct |
19 ms |
1764 KB |
Output is correct |
15 |
Correct |
19 ms |
1732 KB |
Output is correct |
16 |
Correct |
4 ms |
1492 KB |
Output is correct |
17 |
Correct |
16 ms |
2176 KB |
Output is correct |
18 |
Correct |
5 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1044 KB |
Output is correct |
20 |
Correct |
69 ms |
65296 KB |
Output is correct |
21 |
Correct |
7 ms |
1108 KB |
Output is correct |
22 |
Correct |
4 ms |
1108 KB |
Output is correct |
23 |
Correct |
5 ms |
1108 KB |
Output is correct |
24 |
Correct |
17 ms |
1300 KB |
Output is correct |
25 |
Correct |
19 ms |
1180 KB |
Output is correct |
26 |
Correct |
12 ms |
9460 KB |
Output is correct |
27 |
Correct |
13 ms |
5304 KB |
Output is correct |
28 |
Correct |
19 ms |
1484 KB |
Output is correct |
29 |
Correct |
4 ms |
1108 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
980 KB |
Output is correct |
33 |
Correct |
18 ms |
1804 KB |
Output is correct |
34 |
Correct |
19 ms |
1748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
2 ms |
1108 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1164 KB |
Output is correct |
11 |
Correct |
21 ms |
4052 KB |
Output is correct |
12 |
Correct |
4 ms |
1744 KB |
Output is correct |
13 |
Correct |
10 ms |
8388 KB |
Output is correct |
14 |
Correct |
19 ms |
1756 KB |
Output is correct |
15 |
Correct |
19 ms |
1748 KB |
Output is correct |
16 |
Correct |
4 ms |
1492 KB |
Output is correct |
17 |
Correct |
16 ms |
2132 KB |
Output is correct |
18 |
Correct |
5 ms |
1048 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
70 ms |
65184 KB |
Output is correct |
21 |
Correct |
7 ms |
1108 KB |
Output is correct |
22 |
Correct |
4 ms |
1108 KB |
Output is correct |
23 |
Correct |
5 ms |
1172 KB |
Output is correct |
24 |
Correct |
17 ms |
1336 KB |
Output is correct |
25 |
Correct |
19 ms |
1204 KB |
Output is correct |
26 |
Correct |
13 ms |
9416 KB |
Output is correct |
27 |
Correct |
9 ms |
5308 KB |
Output is correct |
28 |
Correct |
19 ms |
1436 KB |
Output is correct |
29 |
Correct |
4 ms |
1108 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
1108 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
18 ms |
1712 KB |
Output is correct |
34 |
Correct |
19 ms |
1740 KB |
Output is correct |
35 |
Execution timed out |
1081 ms |
18988 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
1 ms |
980 KB |
Output is correct |
4 |
Correct |
1 ms |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
980 KB |
Output is correct |
6 |
Correct |
1 ms |
1044 KB |
Output is correct |
7 |
Correct |
1 ms |
980 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
1 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
21 ms |
4120 KB |
Output is correct |
12 |
Correct |
4 ms |
1744 KB |
Output is correct |
13 |
Correct |
10 ms |
8388 KB |
Output is correct |
14 |
Correct |
18 ms |
1760 KB |
Output is correct |
15 |
Correct |
19 ms |
1760 KB |
Output is correct |
16 |
Correct |
4 ms |
1492 KB |
Output is correct |
17 |
Correct |
16 ms |
2084 KB |
Output is correct |
18 |
Correct |
5 ms |
1108 KB |
Output is correct |
19 |
Correct |
2 ms |
1108 KB |
Output is correct |
20 |
Correct |
74 ms |
65284 KB |
Output is correct |
21 |
Correct |
7 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
1108 KB |
Output is correct |
23 |
Correct |
5 ms |
1180 KB |
Output is correct |
24 |
Correct |
18 ms |
1428 KB |
Output is correct |
25 |
Correct |
19 ms |
1168 KB |
Output is correct |
26 |
Correct |
12 ms |
9500 KB |
Output is correct |
27 |
Correct |
9 ms |
5312 KB |
Output is correct |
28 |
Correct |
19 ms |
1484 KB |
Output is correct |
29 |
Correct |
3 ms |
1172 KB |
Output is correct |
30 |
Correct |
1 ms |
980 KB |
Output is correct |
31 |
Correct |
2 ms |
1040 KB |
Output is correct |
32 |
Correct |
2 ms |
1108 KB |
Output is correct |
33 |
Correct |
19 ms |
1748 KB |
Output is correct |
34 |
Correct |
19 ms |
1804 KB |
Output is correct |
35 |
Execution timed out |
1088 ms |
18896 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |