#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 3e4;
const int INF = 1e9;
int N, M, S, E;
vector<int> A[MAXN+10];
vector<pii> adj[MAXN+10];
int dist[MAXN+10];
struct Queue
{
int u, w;
bool operator < (const Queue &p) const { return w>p.w; }
};
int main()
{
int i, j, k;
scanf("%d%d", &N, &M);
for(i=1; i<=M; i++)
{
int b, p;
scanf("%d%d", &b, &p);
if(i==1) S=b;
if(i==2) E=b;
A[p].push_back(b);
}
for(i=1; i<=MAXN; i++)
{
if(A[i].empty()) continue;
vector<vector<int>> V;
vector<int> comp;
V.resize(i);
for(auto it : A[i]) V[it%i].push_back(it);
for(auto it : A[i]) comp.push_back(it%i);
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for(auto it : comp)
{
sort(V[it].begin(), V[it].end());
for(j=1; j<V[it].size(); j++)
{
int now=V[it][j], bef=V[it][j-1];
for(k=bef+i; k<=now; k+=i) adj[bef].push_back({k, (k-bef)/i});
for(k=now-i; k>=bef; k-=i) adj[now].push_back({k, (now-k)/i});
}
for(k=V[it].back()+i; k<N; k+=i) adj[V[it].back()].push_back({k, (k-V[it].back())/i});
for(k=V[it][0]-i; k>=0; k-=i) adj[V[it][0]].push_back({k, (V[it][0]-k)/i});
}
}
priority_queue<Queue> PQ;
for(i=0; i<N; i++) dist[i]=INF;
PQ.push({S, 0}); dist[S]=0;
while(!PQ.empty())
{
Queue now=PQ.top(); PQ.pop();
if(dist[now.u]<now.w) continue;
for(auto nxt : adj[now.u]) if(dist[nxt.first]>now.w+nxt.second) PQ.push({nxt.first, now.w+nxt.second}), dist[nxt.first]=now.w+nxt.second;
}
if(dist[E]==INF) dist[E]=-1;
printf("%d\n", dist[E]);
}
Compilation message
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:51:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(j=1; j<V[it].size(); j++)
~^~~~~~~~~~~~~
skyscraper.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:30:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &b, &p);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1656 KB |
Output is correct |
2 |
Correct |
5 ms |
1656 KB |
Output is correct |
3 |
Correct |
5 ms |
1784 KB |
Output is correct |
4 |
Correct |
5 ms |
1656 KB |
Output is correct |
5 |
Correct |
5 ms |
1656 KB |
Output is correct |
6 |
Correct |
6 ms |
1784 KB |
Output is correct |
7 |
Correct |
6 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1656 KB |
Output is correct |
2 |
Correct |
6 ms |
1656 KB |
Output is correct |
3 |
Correct |
5 ms |
1656 KB |
Output is correct |
4 |
Correct |
6 ms |
1656 KB |
Output is correct |
5 |
Correct |
5 ms |
1656 KB |
Output is correct |
6 |
Correct |
6 ms |
1656 KB |
Output is correct |
7 |
Correct |
5 ms |
1656 KB |
Output is correct |
8 |
Correct |
5 ms |
1784 KB |
Output is correct |
9 |
Correct |
5 ms |
1656 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
7 ms |
1912 KB |
Output is correct |
15 |
Correct |
8 ms |
1784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1660 KB |
Output is correct |
2 |
Correct |
6 ms |
1784 KB |
Output is correct |
3 |
Correct |
5 ms |
1784 KB |
Output is correct |
4 |
Correct |
6 ms |
1656 KB |
Output is correct |
5 |
Correct |
6 ms |
1656 KB |
Output is correct |
6 |
Correct |
5 ms |
1656 KB |
Output is correct |
7 |
Correct |
6 ms |
1656 KB |
Output is correct |
8 |
Correct |
6 ms |
1784 KB |
Output is correct |
9 |
Correct |
5 ms |
1656 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
7 ms |
1784 KB |
Output is correct |
12 |
Correct |
7 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
7 ms |
1784 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
7 ms |
1912 KB |
Output is correct |
18 |
Correct |
9 ms |
1912 KB |
Output is correct |
19 |
Correct |
7 ms |
1784 KB |
Output is correct |
20 |
Correct |
7 ms |
1756 KB |
Output is correct |
21 |
Correct |
9 ms |
1912 KB |
Output is correct |
22 |
Correct |
8 ms |
1912 KB |
Output is correct |
23 |
Correct |
8 ms |
1912 KB |
Output is correct |
24 |
Correct |
11 ms |
2040 KB |
Output is correct |
25 |
Correct |
10 ms |
1912 KB |
Output is correct |
26 |
Correct |
7 ms |
1912 KB |
Output is correct |
27 |
Correct |
7 ms |
1912 KB |
Output is correct |
28 |
Correct |
7 ms |
2040 KB |
Output is correct |
29 |
Correct |
8 ms |
2552 KB |
Output is correct |
30 |
Correct |
6 ms |
2040 KB |
Output is correct |
31 |
Correct |
7 ms |
2168 KB |
Output is correct |
32 |
Correct |
7 ms |
2040 KB |
Output is correct |
33 |
Correct |
11 ms |
3320 KB |
Output is correct |
34 |
Correct |
11 ms |
3320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1656 KB |
Output is correct |
2 |
Correct |
6 ms |
1656 KB |
Output is correct |
3 |
Correct |
8 ms |
1656 KB |
Output is correct |
4 |
Correct |
6 ms |
1656 KB |
Output is correct |
5 |
Correct |
6 ms |
1656 KB |
Output is correct |
6 |
Correct |
5 ms |
1656 KB |
Output is correct |
7 |
Correct |
6 ms |
1784 KB |
Output is correct |
8 |
Correct |
6 ms |
1656 KB |
Output is correct |
9 |
Correct |
6 ms |
1784 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
6 ms |
1784 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
7 ms |
1784 KB |
Output is correct |
15 |
Correct |
7 ms |
1784 KB |
Output is correct |
16 |
Correct |
6 ms |
1784 KB |
Output is correct |
17 |
Correct |
7 ms |
1912 KB |
Output is correct |
18 |
Correct |
8 ms |
1912 KB |
Output is correct |
19 |
Correct |
7 ms |
1784 KB |
Output is correct |
20 |
Correct |
6 ms |
1784 KB |
Output is correct |
21 |
Correct |
8 ms |
1784 KB |
Output is correct |
22 |
Correct |
8 ms |
1912 KB |
Output is correct |
23 |
Correct |
8 ms |
1912 KB |
Output is correct |
24 |
Correct |
11 ms |
1916 KB |
Output is correct |
25 |
Correct |
12 ms |
1912 KB |
Output is correct |
26 |
Correct |
6 ms |
1912 KB |
Output is correct |
27 |
Correct |
7 ms |
1912 KB |
Output is correct |
28 |
Correct |
7 ms |
2040 KB |
Output is correct |
29 |
Correct |
9 ms |
2552 KB |
Output is correct |
30 |
Correct |
7 ms |
2040 KB |
Output is correct |
31 |
Correct |
7 ms |
2168 KB |
Output is correct |
32 |
Correct |
7 ms |
2040 KB |
Output is correct |
33 |
Correct |
11 ms |
3320 KB |
Output is correct |
34 |
Correct |
12 ms |
3320 KB |
Output is correct |
35 |
Correct |
23 ms |
2940 KB |
Output is correct |
36 |
Correct |
13 ms |
2040 KB |
Output is correct |
37 |
Correct |
20 ms |
4088 KB |
Output is correct |
38 |
Correct |
32 ms |
3832 KB |
Output is correct |
39 |
Correct |
32 ms |
3960 KB |
Output is correct |
40 |
Correct |
27 ms |
3832 KB |
Output is correct |
41 |
Correct |
29 ms |
3832 KB |
Output is correct |
42 |
Correct |
13 ms |
2168 KB |
Output is correct |
43 |
Correct |
14 ms |
2168 KB |
Output is correct |
44 |
Correct |
13 ms |
2168 KB |
Output is correct |
45 |
Correct |
28 ms |
6648 KB |
Output is correct |
46 |
Correct |
28 ms |
6536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1656 KB |
Output is correct |
2 |
Correct |
5 ms |
1656 KB |
Output is correct |
3 |
Correct |
5 ms |
1656 KB |
Output is correct |
4 |
Correct |
6 ms |
1656 KB |
Output is correct |
5 |
Correct |
5 ms |
1656 KB |
Output is correct |
6 |
Correct |
6 ms |
1784 KB |
Output is correct |
7 |
Correct |
6 ms |
1656 KB |
Output is correct |
8 |
Correct |
6 ms |
1656 KB |
Output is correct |
9 |
Correct |
6 ms |
1656 KB |
Output is correct |
10 |
Correct |
6 ms |
1784 KB |
Output is correct |
11 |
Correct |
7 ms |
1912 KB |
Output is correct |
12 |
Correct |
6 ms |
1784 KB |
Output is correct |
13 |
Correct |
6 ms |
1784 KB |
Output is correct |
14 |
Correct |
6 ms |
1784 KB |
Output is correct |
15 |
Correct |
6 ms |
1784 KB |
Output is correct |
16 |
Correct |
7 ms |
1784 KB |
Output is correct |
17 |
Correct |
7 ms |
1912 KB |
Output is correct |
18 |
Correct |
8 ms |
1912 KB |
Output is correct |
19 |
Correct |
7 ms |
1784 KB |
Output is correct |
20 |
Correct |
7 ms |
1784 KB |
Output is correct |
21 |
Correct |
8 ms |
1784 KB |
Output is correct |
22 |
Correct |
9 ms |
1912 KB |
Output is correct |
23 |
Correct |
8 ms |
1912 KB |
Output is correct |
24 |
Correct |
11 ms |
1912 KB |
Output is correct |
25 |
Correct |
11 ms |
1912 KB |
Output is correct |
26 |
Correct |
7 ms |
1912 KB |
Output is correct |
27 |
Correct |
8 ms |
1912 KB |
Output is correct |
28 |
Correct |
10 ms |
2040 KB |
Output is correct |
29 |
Correct |
9 ms |
2552 KB |
Output is correct |
30 |
Correct |
7 ms |
2040 KB |
Output is correct |
31 |
Correct |
7 ms |
2168 KB |
Output is correct |
32 |
Correct |
7 ms |
2040 KB |
Output is correct |
33 |
Correct |
11 ms |
3452 KB |
Output is correct |
34 |
Correct |
11 ms |
3320 KB |
Output is correct |
35 |
Correct |
24 ms |
2936 KB |
Output is correct |
36 |
Correct |
13 ms |
2040 KB |
Output is correct |
37 |
Correct |
20 ms |
3960 KB |
Output is correct |
38 |
Correct |
28 ms |
3832 KB |
Output is correct |
39 |
Correct |
28 ms |
3832 KB |
Output is correct |
40 |
Correct |
27 ms |
3832 KB |
Output is correct |
41 |
Correct |
28 ms |
3832 KB |
Output is correct |
42 |
Correct |
15 ms |
2168 KB |
Output is correct |
43 |
Correct |
14 ms |
2168 KB |
Output is correct |
44 |
Correct |
14 ms |
2168 KB |
Output is correct |
45 |
Correct |
28 ms |
6648 KB |
Output is correct |
46 |
Correct |
27 ms |
6520 KB |
Output is correct |
47 |
Correct |
50 ms |
11124 KB |
Output is correct |
48 |
Correct |
390 ms |
5716 KB |
Output is correct |
49 |
Correct |
499 ms |
5024 KB |
Output is correct |
50 |
Correct |
387 ms |
4808 KB |
Output is correct |
51 |
Correct |
644 ms |
7380 KB |
Output is correct |
52 |
Correct |
642 ms |
7448 KB |
Output is correct |
53 |
Correct |
304 ms |
3748 KB |
Output is correct |
54 |
Correct |
8 ms |
2296 KB |
Output is correct |
55 |
Correct |
9 ms |
2804 KB |
Output is correct |
56 |
Correct |
19 ms |
3064 KB |
Output is correct |
57 |
Correct |
39 ms |
11064 KB |
Output is correct |
58 |
Correct |
17 ms |
3072 KB |
Output is correct |
59 |
Correct |
18 ms |
3340 KB |
Output is correct |
60 |
Correct |
20 ms |
3980 KB |
Output is correct |
61 |
Correct |
116 ms |
4264 KB |
Output is correct |
62 |
Correct |
62 ms |
8236 KB |
Output is correct |
63 |
Correct |
79 ms |
24576 KB |
Output is correct |
64 |
Correct |
83 ms |
32756 KB |
Output is correct |
65 |
Correct |
114 ms |
43760 KB |
Output is correct |
66 |
Correct |
209 ms |
71912 KB |
Output is correct |
67 |
Correct |
214 ms |
72036 KB |
Output is correct |