이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<queue>
using namespace std;
#define X first
#define Y second
typedef pair<int, int> Pi;
const int MX = 30003;
const int SX = 0;
priority_queue <Pi, vector<Pi>, greater<Pi> > pq;
vector <Pi> E[MX];
int n, m;
int p[MX][2];
int chk[MX][SX+4];
int len[MX];
int main(){
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++){
scanf("%d%d", p[i], p[i] + 1);
if (p[i][1] < SX)chk[p[i][0]][p[i][1]] = 1;
}
for (int i = 0; i < m; i++){
for (int j = p[i][0] + p[i][1], l = 1; j < n; j += p[i][1], l++){
E[p[i][0]].push_back(Pi(j, l));
if (p[i][1] < SX && chk[j][p[i][1]])break;
}
}
for (int i = 0; i < m; i++){
for (int j = p[i][0] - p[i][1], l = 1; j >= 0; j -= p[i][1], l++){
E[p[i][0]].push_back(Pi(j, l));
if (p[i][1] < SX && chk[j][p[i][1]])break;
}
}
pq.push(Pi(p[0][0], 0));
for (int i = 0; i < n; i++)len[i] = 1e9;
len[p[0][0]] = 0;
while (!pq.empty()){
Pi t = pq.top();
pq.pop();
if (len[t.Y] != t.X)continue;
if (t.Y == p[1][0])break;
for (int i = 0; i < (int)E[t.Y].size(); i++){
Pi &w = E[t.Y][i];
if (len[w.X] > len[t.Y] + w.Y){
len[w.X] = len[t.Y] + w.Y;
pq.push(Pi(len[w.X], w.X));
}
}
}
printf("%d", len[p[1][0]] == 1e9 ? -1 : len[p[1][0]]);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |