#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
#define ff first
#define ss second
#define MP make_pair
const int mxn = 2e5 + 10;
vector<int> adj[mxn];
int a[mxn], n, m;
int dis[2][mxn];
int alt[2][mxn];
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(dis[0], -1, sizeof dis[0]);
memset(dis[1], -1, sizeof dis[1]);
memset(alt[0], -1, sizeof alt[0]);
memset(alt[1], -1, sizeof alt[1]);
priority_queue<pair<pii, int>> pq;
pq.push(MP(MP(0, 0), 1));
while(pq.size()) {
int d = -pq.top().ff.ff;
int mx = pq.top().ff.ss;
int u = pq.top().ss;
pq.pop();
if(dis[0][u] > -1) continue;
dis[0][u] = d;
alt[0][u] = mx;
for(int v : adj[u]) {
if(dis[0][v] > -1) continue;
if(mx >= a[v]) pq.push(MP(MP(-(d + 1), mx), v));
else pq.push(MP(MP(-(d + a[v] - mx), a[v]), v));
}
}
pq.push(MP(MP(0, 0), n));
while(pq.size()) {
int d = -pq.top().ff.ff;
int mx = pq.top().ff.ss;
int u = pq.top().ss;
pq.pop();
if(dis[1][u] > -1) continue;
dis[1][u] = d;
alt[1][u] = mx;
for(int v : adj[u]) {
if(dis[1][v] > -1) continue;
if(mx >= a[v]) pq.push(MP(MP(-(d + 1), mx), v));
else pq.push(MP(MP(-(d + a[v] - mx), a[v]), v));
}
}
int ans = INT_MAX;
for(int i = 1; i <= n; i++)
if(alt[0][i] == alt[1][i] && alt[0][i] != -1)
ans = min(ans, dis[0][i] + dis[1][i]);
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Incorrect |
180 ms |
15100 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
8868 KB |
Output is correct |
6 |
Incorrect |
3 ms |
9052 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
8868 KB |
Output is correct |
6 |
Incorrect |
3 ms |
9052 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Incorrect |
180 ms |
15100 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |