#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<long long> dist(MAXN, inf);
vector<vector<int>> dogos(MAXN);
set<pii> pq;
int doge0;
int doge1;
void 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});
}
}
}
}
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;
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});
}
}
}
dikstra(doge0);
if(dist[doge1] == inf)cout << -1 << endl;
else cout << dist[doge1] << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Correct |
1 ms |
1880 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 |
# |
Verdict |
Execution time |
Memory |
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 |
1896 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 |
2 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
2140 KB |
Output is correct |
12 |
Correct |
5 ms |
4312 KB |
Output is correct |
13 |
Correct |
4 ms |
4308 KB |
Output is correct |
14 |
Correct |
1 ms |
2140 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1880 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 |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
2140 KB |
Output is correct |
12 |
Correct |
4 ms |
4312 KB |
Output is correct |
13 |
Correct |
5 ms |
4308 KB |
Output is correct |
14 |
Correct |
1 ms |
2136 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
2140 KB |
Output is correct |
17 |
Correct |
2 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2140 KB |
Output is correct |
19 |
Correct |
1 ms |
2136 KB |
Output is correct |
20 |
Correct |
44 ms |
34344 KB |
Output is correct |
21 |
Correct |
1 ms |
2140 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
3 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
2140 KB |
Output is correct |
25 |
Correct |
2 ms |
2140 KB |
Output is correct |
26 |
Correct |
42 ms |
36024 KB |
Output is correct |
27 |
Correct |
40 ms |
35008 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
5 ms |
3164 KB |
Output is correct |
34 |
Correct |
5 ms |
3164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
1 ms |
1884 KB |
Output is correct |
9 |
Correct |
1 ms |
1884 KB |
Output is correct |
10 |
Correct |
1 ms |
1884 KB |
Output is correct |
11 |
Correct |
1 ms |
2140 KB |
Output is correct |
12 |
Correct |
4 ms |
4312 KB |
Output is correct |
13 |
Correct |
4 ms |
4308 KB |
Output is correct |
14 |
Correct |
1 ms |
2140 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
2136 KB |
Output is correct |
17 |
Correct |
2 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2140 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
43 ms |
34128 KB |
Output is correct |
21 |
Correct |
1 ms |
1880 KB |
Output is correct |
22 |
Correct |
1 ms |
2140 KB |
Output is correct |
23 |
Correct |
1 ms |
2140 KB |
Output is correct |
24 |
Correct |
2 ms |
2224 KB |
Output is correct |
25 |
Correct |
2 ms |
2140 KB |
Output is correct |
26 |
Correct |
44 ms |
36640 KB |
Output is correct |
27 |
Correct |
40 ms |
35008 KB |
Output is correct |
28 |
Correct |
2 ms |
2396 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2396 KB |
Output is correct |
31 |
Correct |
3 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
5 ms |
3164 KB |
Output is correct |
34 |
Correct |
5 ms |
3164 KB |
Output is correct |
35 |
Correct |
6 ms |
3764 KB |
Output is correct |
36 |
Correct |
2 ms |
2140 KB |
Output is correct |
37 |
Correct |
9 ms |
5212 KB |
Output is correct |
38 |
Correct |
9 ms |
4724 KB |
Output is correct |
39 |
Correct |
9 ms |
4956 KB |
Output is correct |
40 |
Correct |
9 ms |
4656 KB |
Output is correct |
41 |
Correct |
8 ms |
4700 KB |
Output is correct |
42 |
Runtime error |
265 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1880 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 |
1 ms |
1884 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 |
4 ms |
4312 KB |
Output is correct |
13 |
Correct |
4 ms |
4308 KB |
Output is correct |
14 |
Correct |
1 ms |
2140 KB |
Output is correct |
15 |
Correct |
1 ms |
2140 KB |
Output is correct |
16 |
Correct |
1 ms |
2140 KB |
Output is correct |
17 |
Correct |
2 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2140 KB |
Output is correct |
19 |
Correct |
1 ms |
2140 KB |
Output is correct |
20 |
Correct |
46 ms |
34168 KB |
Output is correct |
21 |
Correct |
1 ms |
1884 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 |
2136 KB |
Output is correct |
25 |
Correct |
2 ms |
2136 KB |
Output is correct |
26 |
Correct |
46 ms |
36060 KB |
Output is correct |
27 |
Correct |
49 ms |
34996 KB |
Output is correct |
28 |
Correct |
2 ms |
2392 KB |
Output is correct |
29 |
Correct |
4 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2420 KB |
Output is correct |
31 |
Correct |
2 ms |
2396 KB |
Output is correct |
32 |
Correct |
2 ms |
2396 KB |
Output is correct |
33 |
Correct |
6 ms |
3164 KB |
Output is correct |
34 |
Correct |
5 ms |
3160 KB |
Output is correct |
35 |
Correct |
6 ms |
3828 KB |
Output is correct |
36 |
Correct |
2 ms |
2396 KB |
Output is correct |
37 |
Correct |
8 ms |
5212 KB |
Output is correct |
38 |
Correct |
8 ms |
4572 KB |
Output is correct |
39 |
Correct |
8 ms |
4952 KB |
Output is correct |
40 |
Correct |
8 ms |
4696 KB |
Output is correct |
41 |
Correct |
8 ms |
4700 KB |
Output is correct |
42 |
Runtime error |
259 ms |
262144 KB |
Execution killed with signal 9 |
43 |
Halted |
0 ms |
0 KB |
- |