#include <bits/stdc++.h>
using namespace std;
int n,m;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
int a[n];
for(int &i : a){
cin >> i;
}
vector<int>g[n];
for(int i = 0;i<m;i++){
int a,b;
cin >> a >> b;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
set<array<int,4>>pq;
pq.insert({0,0,0,0});
//time,current height, place, downbool
set<array<int,3>>vis;
//height,place,downbool
set<int>vis1;
long long ans = 0;
while(!pq.empty()){
array<int,4>cur = *(pq.begin());
//cout << cur[0] << " " << cur[1] << " " << cur[2] << " " << cur[3] << "\n";
pq.erase(pq.begin());
if(vis.find({cur[1],cur[2],cur[3]})!=vis.end()){
continue;
}
vis.insert({cur[1],cur[2],cur[3]});
vis1.insert(cur[2]);
if(cur[2]==n-1){
pq.insert({cur[0]+cur[1],0,cur[2],1});
if(cur[1]==0){
ans=cur[0];
break;
}
}
for(int i : g[cur[2]]){
if(vis1.find(i)!=vis1.end()){
continue;
}
if(cur[3]){
if(a[i]<=cur[1]-1){
pq.insert({cur[0]+1,cur[1]-1,i,1});
}
else if(a[i]<=cur[1]){
pq.insert({cur[0]+1,cur[1],i,1});
}
}
else{
if(a[i]>cur[1]){
pq.insert({a[i]-cur[1]+cur[0],a[i],i,0});
}
else{
pq.insert({cur[0]+1,cur[1]+1,i,0});
}
if(a[i]<=cur[1]-1){
pq.insert({cur[0]+1,cur[1]-1,i,1});
}
else if(a[i]<=cur[1]){
pq.insert({cur[0]+1,cur[1],i,1});
}
}
}
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Execution timed out |
1096 ms |
98980 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
29 ms |
3932 KB |
Output is correct |
7 |
Correct |
9 ms |
1616 KB |
Output is correct |
8 |
Correct |
19 ms |
2640 KB |
Output is correct |
9 |
Correct |
3 ms |
848 KB |
Output is correct |
10 |
Correct |
10 ms |
1616 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
11 ms |
1600 KB |
Output is correct |
13 |
Correct |
4 ms |
848 KB |
Output is correct |
14 |
Correct |
5 ms |
848 KB |
Output is correct |
15 |
Correct |
5 ms |
848 KB |
Output is correct |
16 |
Correct |
5 ms |
848 KB |
Output is correct |
17 |
Incorrect |
7 ms |
1144 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
396 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
29 ms |
3932 KB |
Output is correct |
7 |
Correct |
9 ms |
1616 KB |
Output is correct |
8 |
Correct |
19 ms |
2640 KB |
Output is correct |
9 |
Correct |
3 ms |
848 KB |
Output is correct |
10 |
Correct |
10 ms |
1616 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
11 ms |
1600 KB |
Output is correct |
13 |
Correct |
4 ms |
848 KB |
Output is correct |
14 |
Correct |
5 ms |
848 KB |
Output is correct |
15 |
Correct |
5 ms |
848 KB |
Output is correct |
16 |
Correct |
5 ms |
848 KB |
Output is correct |
17 |
Incorrect |
7 ms |
1144 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Execution timed out |
1096 ms |
98980 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |