#include <bits/stdc++.h>
#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>
using namespace std;
const int MAXN = 30005;
const int mod7 = 1e9+7;
const long long inf = 1e18;
vector<vector<pii>> graf(MAXN);
vector<int> dist(MAXN, inf);
vector<vector<int>> dogos(MAXN);
set<pii> pq;
int doge0;
int doge1;
int dikstra(int nod)
{
dist[nod] = 0;
pq.insert({dist[nod], nod});
while(!pq.empty())
{
auto it = pq.begin();
int u = it-> sc;
pq.erase(it);
for(auto x: graf[u])
{
if(dist[x.fi] > dist[u] + x.sc)
{
pq.erase({dist[x.fi], x.fi});
dist[x.fi] = dist[u] + x.sc;
pq.insert({dist[x.fi], x.fi});
}
}
}
return dist[doge1];
}
signed main()
{
ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
int tt=1;
//cin >> tt;
while(tt--)
{
int n,m;
cin >> n >> m;
vector<pii> niz(m);
for(int i=0; i<m; i++)
{
int a,b;cin >> a >> b;
if(i==0)doge0 = a;
if(i==1)doge1 = a;
dogos[a].pb(b);
}
for(int i=0; i<n; i++)
{
for(auto d: dogos[i])
{
int j = i;
while(j+ d <n)
{
j+=d;
graf[i].pb({j, (j-i)/d});
}
j = i;
while(j- d >=0)
{
j-=d;
graf[i].pb({j,(i-j)/d});
}
}
}
if(dikstra(doge0) == inf)cout << -1 << endl;
else cout << dist[doge1] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
2 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
2 ms |
2140 KB |
Output is correct |
11 |
Correct |
2 ms |
2140 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
5 ms |
6868 KB |
Output is correct |
14 |
Correct |
1 ms |
2140 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Correct |
1 ms |
1884 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
2116 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1880 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
2140 KB |
Output is correct |
11 |
Correct |
1 ms |
2140 KB |
Output is correct |
12 |
Correct |
5 ms |
6844 KB |
Output is correct |
13 |
Correct |
5 ms |
7892 KB |
Output is correct |
14 |
Correct |
1 ms |
2140 KB |
Output is correct |
15 |
Correct |
2 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
2096 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2212 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
61 ms |
66240 KB |
Output is correct |
21 |
Correct |
1 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
2 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
2416 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
69 ms |
67868 KB |
Output is correct |
27 |
Correct |
64 ms |
68008 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
4 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
2400 KB |
Output is correct |
31 |
Correct |
3 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2660 KB |
Output is correct |
33 |
Correct |
6 ms |
4312 KB |
Output is correct |
34 |
Correct |
6 ms |
4316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1892 KB |
Output is correct |
2 |
Correct |
1 ms |
1896 KB |
Output is correct |
3 |
Correct |
1 ms |
1884 KB |
Output is correct |
4 |
Correct |
1 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1892 KB |
Output is correct |
6 |
Correct |
1 ms |
1996 KB |
Output is correct |
7 |
Correct |
1 ms |
1888 KB |
Output is correct |
8 |
Correct |
1 ms |
1888 KB |
Output is correct |
9 |
Correct |
1 ms |
1896 KB |
Output is correct |
10 |
Correct |
1 ms |
2144 KB |
Output is correct |
11 |
Correct |
1 ms |
2152 KB |
Output is correct |
12 |
Correct |
4 ms |
6828 KB |
Output is correct |
13 |
Correct |
7 ms |
7124 KB |
Output is correct |
14 |
Correct |
1 ms |
2140 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
16 |
Correct |
2 ms |
2140 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2124 KB |
Output is correct |
19 |
Correct |
1 ms |
2136 KB |
Output is correct |
20 |
Correct |
60 ms |
66344 KB |
Output is correct |
21 |
Correct |
2 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
2 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2392 KB |
Output is correct |
26 |
Correct |
71 ms |
68012 KB |
Output is correct |
27 |
Correct |
63 ms |
67996 KB |
Output is correct |
28 |
Correct |
2 ms |
2648 KB |
Output is correct |
29 |
Correct |
4 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
2372 KB |
Output is correct |
31 |
Correct |
3 ms |
2908 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
6 ms |
4312 KB |
Output is correct |
34 |
Correct |
6 ms |
4312 KB |
Output is correct |
35 |
Correct |
8 ms |
5724 KB |
Output is correct |
36 |
Correct |
3 ms |
2708 KB |
Output is correct |
37 |
Correct |
10 ms |
8796 KB |
Output is correct |
38 |
Correct |
16 ms |
7772 KB |
Output is correct |
39 |
Correct |
10 ms |
8028 KB |
Output is correct |
40 |
Correct |
15 ms |
8028 KB |
Output is correct |
41 |
Correct |
11 ms |
7680 KB |
Output is correct |
42 |
Runtime error |
221 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2112 KB |
Output is correct |
2 |
Correct |
2 ms |
2120 KB |
Output is correct |
3 |
Correct |
2 ms |
1884 KB |
Output is correct |
4 |
Correct |
2 ms |
1884 KB |
Output is correct |
5 |
Correct |
1 ms |
1884 KB |
Output is correct |
6 |
Correct |
1 ms |
1884 KB |
Output is correct |
7 |
Correct |
1 ms |
1884 KB |
Output is correct |
8 |
Correct |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
2112 KB |
Output is correct |
11 |
Correct |
2 ms |
2204 KB |
Output is correct |
12 |
Correct |
5 ms |
8140 KB |
Output is correct |
13 |
Correct |
6 ms |
6612 KB |
Output is correct |
14 |
Correct |
1 ms |
2284 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
2152 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
1 ms |
2140 KB |
Output is correct |
19 |
Correct |
2 ms |
2140 KB |
Output is correct |
20 |
Correct |
62 ms |
66460 KB |
Output is correct |
21 |
Correct |
1 ms |
2136 KB |
Output is correct |
22 |
Correct |
2 ms |
2140 KB |
Output is correct |
23 |
Correct |
2 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
71 ms |
68048 KB |
Output is correct |
27 |
Correct |
64 ms |
68016 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
6 ms |
3164 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
4 ms |
2908 KB |
Output is correct |
32 |
Correct |
3 ms |
2648 KB |
Output is correct |
33 |
Correct |
6 ms |
4312 KB |
Output is correct |
34 |
Correct |
7 ms |
4356 KB |
Output is correct |
35 |
Correct |
12 ms |
5980 KB |
Output is correct |
36 |
Correct |
4 ms |
2652 KB |
Output is correct |
37 |
Correct |
10 ms |
8796 KB |
Output is correct |
38 |
Correct |
10 ms |
7796 KB |
Output is correct |
39 |
Correct |
11 ms |
8256 KB |
Output is correct |
40 |
Correct |
16 ms |
8096 KB |
Output is correct |
41 |
Correct |
10 ms |
7768 KB |
Output is correct |
42 |
Runtime error |
216 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |