#include <bits/stdc++.h>
#define maxn 30005
#define INF maxn*maxn+20
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
int n,m;
pii dg[maxn];
vector<pii>adj[maxn];
priority_queue<pii>pq;
int dis[maxn];
bool bio[maxn];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>m;
for(int i=0;i<m;i++)
{
cin>>dg[i].f>>dg[i].s;
}
sort(dg,dg+m);
for(int i=0;i<m;i++)
{
for(int j=i+1;j<m;j++)
{
int dis=dg[j].f-dg[i].f;
if(dis%dg[i].s!=0)continue;
adj[i].push_back({j,dis/dg[i].s});
if(dg[i].s==dg[j].s)break;
}
for(int j=i-1;j>=0;j--)
{
int dis=dg[i].f-dg[j].f;
if(dis%dg[i].s!=0)continue;
adj[i].push_back({j,dis/dg[i].s});
if(dg[i].s==dg[j].s)break;
}
}
/*for(int i=0;i<m;i++)
{
cout<<i<<" ";
for(auto x:adj[i])cout<<x.f<<","<<x.s<<" ";
cout<<endl;
}*/
dis[0]=0;
pq.push({0,0});
bio[0]=false;
for(int i=1;i<m;i++)
{
dis[i]=INF;
bio[i]=false;
}
while(!pq.empty())
{
int v=pq.top().s;
pq.pop();
if(bio[v])continue;
bio[v]=true;
for(auto x:adj[v])
{
int u=x.f;
int w=x.s;
if(dis[v]+w<dis[u])
{
dis[u]=dis[v]+w;
pq.push({-dis[u],u});
}
}
}
if(dis[1]==INF)cout<<"-1"<<endl;
else cout<<dis[1]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
3 ms |
1144 KB |
Output is correct |
3 |
Correct |
2 ms |
1200 KB |
Output is correct |
4 |
Correct |
3 ms |
1260 KB |
Output is correct |
5 |
Correct |
2 ms |
1260 KB |
Output is correct |
6 |
Correct |
2 ms |
1328 KB |
Output is correct |
7 |
Incorrect |
3 ms |
1328 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1328 KB |
Output is correct |
2 |
Correct |
3 ms |
1328 KB |
Output is correct |
3 |
Correct |
2 ms |
1364 KB |
Output is correct |
4 |
Correct |
3 ms |
1364 KB |
Output is correct |
5 |
Correct |
3 ms |
1364 KB |
Output is correct |
6 |
Correct |
3 ms |
1432 KB |
Output is correct |
7 |
Incorrect |
2 ms |
1432 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1432 KB |
Output is correct |
2 |
Correct |
3 ms |
1432 KB |
Output is correct |
3 |
Correct |
2 ms |
1432 KB |
Output is correct |
4 |
Correct |
3 ms |
1456 KB |
Output is correct |
5 |
Correct |
3 ms |
1456 KB |
Output is correct |
6 |
Correct |
2 ms |
1456 KB |
Output is correct |
7 |
Incorrect |
2 ms |
1456 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1456 KB |
Output is correct |
2 |
Correct |
3 ms |
1456 KB |
Output is correct |
3 |
Correct |
3 ms |
1456 KB |
Output is correct |
4 |
Correct |
2 ms |
1456 KB |
Output is correct |
5 |
Correct |
2 ms |
1456 KB |
Output is correct |
6 |
Correct |
2 ms |
1456 KB |
Output is correct |
7 |
Incorrect |
2 ms |
1456 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1456 KB |
Output is correct |
2 |
Correct |
2 ms |
1456 KB |
Output is correct |
3 |
Correct |
2 ms |
1456 KB |
Output is correct |
4 |
Correct |
3 ms |
1456 KB |
Output is correct |
5 |
Correct |
3 ms |
1456 KB |
Output is correct |
6 |
Correct |
3 ms |
1456 KB |
Output is correct |
7 |
Incorrect |
3 ms |
1456 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |