#include <bits/stdc++.h>
using namespace std;
long long n,p[30004],b[30004],m;
priority_queue <pair<long long,long long> ,vector <pair<long long,long long> >,greater <pair<long long,long long> > > pq;
bool vis[30004];
vector <long long> v[30004];
long long cur[30004];
void dij()
{
for (int i=0;i<n;i++)
cur[i]=LONG_LONG_MAX;
for (int i=0;i<v[b[0]].size();i++)
cur[v[b[0]][i]]=0,pq.push({0,v[b[0]][i]});
while (!pq.empty())
{
long long x= (pq.top()).second;
long long y= (pq.top()).first;
pq.pop();
if (vis[x])
continue;
vis[x]=true;
for (long long i=0;i*p[x]+b[x]<n;i++)
{
long long a= (i*p[x]+b[x]);
for (int j=0;j<v[a].size();j++)
{
if (cur[v[a][j]]>i+y)
{
pq.push({i+y,v[a][j]});
cur[v[a][j]]=i+y;
}
}
}
for (long long i=1;b[x]-i*p[x]>=0;i++)
{
long long a= (b[x]-i*p[x]);
for (int j=0;j<v[a].size();j++)
{
if (cur[v[a][j]]>i+y)
{
pq.push({i+y,v[a][j]});
cur[v[a][j]]=i+y;
}
}
}
}
}
int main()
{
cin >>n>>m;
for (int i=0;i<m;i++)
{
cin >>b[i]>>p[i];
v[b[i]].push_back(i);
}
dij();
if (cur[1]==LONG_LONG_MAX)
cout <<-1;
else
cout <<cur[1];
return 0;
}
Compilation message
skyscraper.cpp: In function 'void dij()':
skyscraper.cpp:12:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<v[b[0]].size();i++)
^
skyscraper.cpp:25:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0;j<v[a].size();j++)
^
skyscraper.cpp:37:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j=0;j<v[a].size();j++)
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
2 ms |
1120 KB |
Output is correct |
3 |
Correct |
2 ms |
1120 KB |
Output is correct |
4 |
Correct |
2 ms |
1120 KB |
Output is correct |
5 |
Correct |
2 ms |
1120 KB |
Output is correct |
6 |
Correct |
2 ms |
1120 KB |
Output is correct |
7 |
Correct |
2 ms |
1120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1120 KB |
Output is correct |
2 |
Correct |
2 ms |
1208 KB |
Output is correct |
3 |
Correct |
2 ms |
1208 KB |
Output is correct |
4 |
Correct |
2 ms |
1264 KB |
Output is correct |
5 |
Correct |
2 ms |
1264 KB |
Output is correct |
6 |
Correct |
2 ms |
1264 KB |
Output is correct |
7 |
Correct |
2 ms |
1264 KB |
Output is correct |
8 |
Correct |
2 ms |
1264 KB |
Output is correct |
9 |
Correct |
2 ms |
1316 KB |
Output is correct |
10 |
Incorrect |
2 ms |
1316 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1316 KB |
Output is correct |
2 |
Correct |
2 ms |
1316 KB |
Output is correct |
3 |
Correct |
2 ms |
1316 KB |
Output is correct |
4 |
Correct |
2 ms |
1316 KB |
Output is correct |
5 |
Correct |
2 ms |
1316 KB |
Output is correct |
6 |
Correct |
2 ms |
1316 KB |
Output is correct |
7 |
Correct |
2 ms |
1316 KB |
Output is correct |
8 |
Correct |
2 ms |
1316 KB |
Output is correct |
9 |
Correct |
2 ms |
1316 KB |
Output is correct |
10 |
Incorrect |
2 ms |
1316 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1316 KB |
Output is correct |
2 |
Correct |
2 ms |
1316 KB |
Output is correct |
3 |
Correct |
2 ms |
1316 KB |
Output is correct |
4 |
Correct |
2 ms |
1316 KB |
Output is correct |
5 |
Correct |
2 ms |
1316 KB |
Output is correct |
6 |
Correct |
2 ms |
1316 KB |
Output is correct |
7 |
Correct |
2 ms |
1316 KB |
Output is correct |
8 |
Correct |
2 ms |
1316 KB |
Output is correct |
9 |
Correct |
2 ms |
1316 KB |
Output is correct |
10 |
Incorrect |
2 ms |
1316 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1316 KB |
Output is correct |
2 |
Correct |
2 ms |
1316 KB |
Output is correct |
3 |
Correct |
2 ms |
1316 KB |
Output is correct |
4 |
Correct |
2 ms |
1316 KB |
Output is correct |
5 |
Correct |
2 ms |
1316 KB |
Output is correct |
6 |
Correct |
2 ms |
1316 KB |
Output is correct |
7 |
Correct |
2 ms |
1320 KB |
Output is correct |
8 |
Correct |
2 ms |
1320 KB |
Output is correct |
9 |
Correct |
2 ms |
1320 KB |
Output is correct |
10 |
Incorrect |
2 ms |
1320 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |