#include <bits/stdc++.h>
using namespace std;
#define ll long long
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define usecppio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(x) ((x).begin()),((x).end())
using pii = pair <int, int>;
#define INF 0x7f7f7f7f
const int MX = 30202;
struct Edge
{
int dest, w;
bool operator<(const Edge &p) const
{
return w > p.w;
}
};
bool relax(Edge edge, int u, int dist[])
{
bool flag = 0;
int v = edge.dest, w = edge.w;
if (dist[v] > dist[u] + w && (dist[u]!=INF))
{
flag = true;
dist[v] = dist[u]+w;
}
return flag;
}
void dijkstra(int dist[], int start, vector<Edge> graph[])
{
fill(dist,dist+MX,INF);
dist[start] = 0;
priority_queue<Edge> pq;
pq.push({start,0});
while(!pq.empty())
{
Edge x = pq.top();
int v = x.dest, w = x.w;
pq.pop();
if (w>dist[v])
continue;
for (auto ed : graph[v])
if (relax(ed, v, dist))
pq.push({ed.dest,dist[ed.dest]});
}
}
struct doge
{
int start, power, index;
};
int n, m;
vector <int> bi(30303, 0), pi(30303, 0);
vector <Edge> G[30303];
bitset <30303> mark[30303];
vector <doge> alldoge[30303];
vector <doge> dogeinfo;
int distances[30303];
bool dogevisit[30303];
void dfs(doge curdoge)
{
dogevisit[curdoge.index] = true;
for (auto it:alldoge[curdoge.start])
if (!dogevisit[it.index])
dfs(it);
for (int i = 1; ;i++)
{
int u = curdoge.start+i*curdoge.power;
if (u >= n)
break;
bool check = false;
for (auto it:alldoge[u])
{
if (it.power == curdoge.power)
check = true;
if (!dogevisit[it.index])
dfs(it);
}
G[curdoge.start].push_back({u,i});
if (check)
break;
}
for (int i = -1; ;i--)
{
int u = curdoge.start+i*curdoge.power;
if (u < 0)
break;
bool check = false;
for (auto it:alldoge[u])
{
if (it.power == curdoge.power)
check = true;
if (!dogevisit[it.index])
dfs(it);
}
G[curdoge.start].push_back({u,-i});
if (check) break;
}
}
int32_t main()
{
usecppio
cin >> n >> m;
for (int i = 0; i<m; i++)
{
cin >> bi[i] >> pi[i];
if (!mark[bi[i]][pi[i]] && i!=1)
{
mark[bi[i]][pi[i]] = true;
alldoge[bi[i]].push_back({bi[i], pi[i], i});
}
dogeinfo.push_back({bi[i], pi[i], i});
}
dogevisit[0] = true;
dfs(dogeinfo[0]);
dijkstra(distances,bi[0],G);
if (distances[bi[1]] >= 10000000)
printf("-1");
else printf("%d\n",distances[bi[1]]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2168 KB |
Output is correct |
2 |
Correct |
5 ms |
2168 KB |
Output is correct |
3 |
Correct |
6 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
6 ms |
2168 KB |
Output is correct |
6 |
Correct |
6 ms |
2168 KB |
Output is correct |
7 |
Correct |
6 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2168 KB |
Output is correct |
2 |
Correct |
6 ms |
2168 KB |
Output is correct |
3 |
Correct |
6 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2172 KB |
Output is correct |
5 |
Correct |
6 ms |
2168 KB |
Output is correct |
6 |
Correct |
6 ms |
2168 KB |
Output is correct |
7 |
Correct |
6 ms |
2168 KB |
Output is correct |
8 |
Correct |
6 ms |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2296 KB |
Output is correct |
10 |
Correct |
7 ms |
2552 KB |
Output is correct |
11 |
Correct |
7 ms |
2936 KB |
Output is correct |
12 |
Correct |
6 ms |
2168 KB |
Output is correct |
13 |
Correct |
7 ms |
2552 KB |
Output is correct |
14 |
Correct |
7 ms |
2552 KB |
Output is correct |
15 |
Correct |
7 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
2168 KB |
Output is correct |
2 |
Correct |
6 ms |
2168 KB |
Output is correct |
3 |
Correct |
6 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
5 ms |
2168 KB |
Output is correct |
6 |
Correct |
6 ms |
2168 KB |
Output is correct |
7 |
Correct |
6 ms |
2168 KB |
Output is correct |
8 |
Correct |
6 ms |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2552 KB |
Output is correct |
11 |
Correct |
7 ms |
2936 KB |
Output is correct |
12 |
Correct |
6 ms |
2168 KB |
Output is correct |
13 |
Correct |
6 ms |
2552 KB |
Output is correct |
14 |
Correct |
6 ms |
2552 KB |
Output is correct |
15 |
Correct |
8 ms |
2552 KB |
Output is correct |
16 |
Correct |
7 ms |
2936 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
7 ms |
4088 KB |
Output is correct |
20 |
Correct |
12 ms |
9976 KB |
Output is correct |
21 |
Correct |
7 ms |
3576 KB |
Output is correct |
22 |
Correct |
8 ms |
4472 KB |
Output is correct |
23 |
Correct |
8 ms |
5112 KB |
Output is correct |
24 |
Correct |
10 ms |
7032 KB |
Output is correct |
25 |
Correct |
10 ms |
7160 KB |
Output is correct |
26 |
Correct |
7 ms |
2552 KB |
Output is correct |
27 |
Correct |
6 ms |
2552 KB |
Output is correct |
28 |
Correct |
12 ms |
9976 KB |
Output is correct |
29 |
Correct |
8 ms |
3064 KB |
Output is correct |
30 |
Correct |
7 ms |
2552 KB |
Output is correct |
31 |
Correct |
7 ms |
2680 KB |
Output is correct |
32 |
Correct |
8 ms |
2552 KB |
Output is correct |
33 |
Correct |
10 ms |
3704 KB |
Output is correct |
34 |
Correct |
10 ms |
3704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2168 KB |
Output is correct |
2 |
Correct |
6 ms |
2168 KB |
Output is correct |
3 |
Correct |
6 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
6 ms |
2168 KB |
Output is correct |
6 |
Correct |
6 ms |
2172 KB |
Output is correct |
7 |
Correct |
6 ms |
2168 KB |
Output is correct |
8 |
Correct |
6 ms |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2552 KB |
Output is correct |
11 |
Correct |
7 ms |
2936 KB |
Output is correct |
12 |
Correct |
6 ms |
2168 KB |
Output is correct |
13 |
Correct |
6 ms |
2552 KB |
Output is correct |
14 |
Correct |
6 ms |
2552 KB |
Output is correct |
15 |
Correct |
6 ms |
2552 KB |
Output is correct |
16 |
Correct |
6 ms |
2936 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
7 ms |
4088 KB |
Output is correct |
20 |
Correct |
12 ms |
9980 KB |
Output is correct |
21 |
Correct |
8 ms |
3576 KB |
Output is correct |
22 |
Correct |
7 ms |
4472 KB |
Output is correct |
23 |
Correct |
9 ms |
5112 KB |
Output is correct |
24 |
Correct |
11 ms |
7032 KB |
Output is correct |
25 |
Correct |
11 ms |
7160 KB |
Output is correct |
26 |
Correct |
7 ms |
2552 KB |
Output is correct |
27 |
Correct |
6 ms |
2552 KB |
Output is correct |
28 |
Correct |
13 ms |
9976 KB |
Output is correct |
29 |
Correct |
8 ms |
3064 KB |
Output is correct |
30 |
Correct |
7 ms |
2552 KB |
Output is correct |
31 |
Correct |
7 ms |
2808 KB |
Output is correct |
32 |
Correct |
7 ms |
2680 KB |
Output is correct |
33 |
Correct |
10 ms |
3704 KB |
Output is correct |
34 |
Correct |
9 ms |
3704 KB |
Output is correct |
35 |
Correct |
27 ms |
13236 KB |
Output is correct |
36 |
Correct |
11 ms |
6904 KB |
Output is correct |
37 |
Correct |
29 ms |
15284 KB |
Output is correct |
38 |
Correct |
34 ms |
17076 KB |
Output is correct |
39 |
Correct |
34 ms |
17076 KB |
Output is correct |
40 |
Correct |
33 ms |
17076 KB |
Output is correct |
41 |
Correct |
35 ms |
17076 KB |
Output is correct |
42 |
Correct |
11 ms |
3192 KB |
Output is correct |
43 |
Correct |
10 ms |
3060 KB |
Output is correct |
44 |
Correct |
17 ms |
10632 KB |
Output is correct |
45 |
Correct |
31 ms |
8500 KB |
Output is correct |
46 |
Correct |
32 ms |
8628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2168 KB |
Output is correct |
2 |
Correct |
6 ms |
2168 KB |
Output is correct |
3 |
Correct |
6 ms |
2168 KB |
Output is correct |
4 |
Correct |
6 ms |
2168 KB |
Output is correct |
5 |
Correct |
6 ms |
2168 KB |
Output is correct |
6 |
Correct |
6 ms |
2168 KB |
Output is correct |
7 |
Correct |
6 ms |
2168 KB |
Output is correct |
8 |
Correct |
6 ms |
2168 KB |
Output is correct |
9 |
Correct |
6 ms |
2168 KB |
Output is correct |
10 |
Correct |
6 ms |
2552 KB |
Output is correct |
11 |
Correct |
6 ms |
2808 KB |
Output is correct |
12 |
Correct |
6 ms |
2168 KB |
Output is correct |
13 |
Correct |
6 ms |
2552 KB |
Output is correct |
14 |
Correct |
6 ms |
2424 KB |
Output is correct |
15 |
Correct |
7 ms |
2552 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
9 ms |
5112 KB |
Output is correct |
18 |
Correct |
8 ms |
5112 KB |
Output is correct |
19 |
Correct |
7 ms |
4088 KB |
Output is correct |
20 |
Correct |
12 ms |
9976 KB |
Output is correct |
21 |
Correct |
7 ms |
3576 KB |
Output is correct |
22 |
Correct |
8 ms |
4472 KB |
Output is correct |
23 |
Correct |
9 ms |
5116 KB |
Output is correct |
24 |
Correct |
10 ms |
7032 KB |
Output is correct |
25 |
Correct |
10 ms |
7160 KB |
Output is correct |
26 |
Correct |
7 ms |
2552 KB |
Output is correct |
27 |
Correct |
7 ms |
2552 KB |
Output is correct |
28 |
Correct |
12 ms |
10104 KB |
Output is correct |
29 |
Correct |
8 ms |
3068 KB |
Output is correct |
30 |
Correct |
7 ms |
2552 KB |
Output is correct |
31 |
Correct |
7 ms |
2680 KB |
Output is correct |
32 |
Correct |
7 ms |
2552 KB |
Output is correct |
33 |
Correct |
9 ms |
3708 KB |
Output is correct |
34 |
Correct |
10 ms |
3704 KB |
Output is correct |
35 |
Correct |
27 ms |
13108 KB |
Output is correct |
36 |
Correct |
11 ms |
6776 KB |
Output is correct |
37 |
Correct |
29 ms |
15152 KB |
Output is correct |
38 |
Correct |
34 ms |
16820 KB |
Output is correct |
39 |
Correct |
34 ms |
16820 KB |
Output is correct |
40 |
Correct |
35 ms |
16820 KB |
Output is correct |
41 |
Correct |
33 ms |
16820 KB |
Output is correct |
42 |
Correct |
11 ms |
3192 KB |
Output is correct |
43 |
Correct |
11 ms |
3064 KB |
Output is correct |
44 |
Correct |
16 ms |
10632 KB |
Output is correct |
45 |
Correct |
31 ms |
8500 KB |
Output is correct |
46 |
Correct |
31 ms |
8632 KB |
Output is correct |
47 |
Correct |
83 ms |
56372 KB |
Output is correct |
48 |
Correct |
54 ms |
58928 KB |
Output is correct |
49 |
Correct |
54 ms |
58932 KB |
Output is correct |
50 |
Correct |
44 ms |
48120 KB |
Output is correct |
51 |
Correct |
100 ms |
82100 KB |
Output is correct |
52 |
Correct |
102 ms |
82740 KB |
Output is correct |
53 |
Correct |
80 ms |
77364 KB |
Output is correct |
54 |
Correct |
8 ms |
2552 KB |
Output is correct |
55 |
Correct |
8 ms |
2552 KB |
Output is correct |
56 |
Correct |
101 ms |
121012 KB |
Output is correct |
57 |
Correct |
48 ms |
20852 KB |
Output is correct |
58 |
Correct |
13 ms |
3444 KB |
Output is correct |
59 |
Correct |
16 ms |
4344 KB |
Output is correct |
60 |
Correct |
18 ms |
5884 KB |
Output is correct |
61 |
Correct |
23 ms |
6284 KB |
Output is correct |
62 |
Correct |
111 ms |
121692 KB |
Output is correct |
63 |
Correct |
74 ms |
27184 KB |
Output is correct |
64 |
Correct |
81 ms |
30512 KB |
Output is correct |
65 |
Correct |
105 ms |
39856 KB |
Output is correct |
66 |
Correct |
165 ms |
65832 KB |
Output is correct |
67 |
Correct |
167 ms |
66596 KB |
Output is correct |