#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()
typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;
const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;
int enc(int x, int y) {
return x * 2001 + y;
}
pii dec(int x) {
return {x / 2001, x % 2001};
}
const int mxn = 2001 * 2001;
vector<int> adj[mxn]; int d[mxn];
inline void solve() {
int n, m;
cin >> n >> m;
memset(d, -1, sizeof(d));
vi a(n); for (int i = 0; i < n; i++) cin >> a[i];
vector<pii> edges; for (int i = 0; i < m; i++) {
int u, v; cin >> u >> v; u--; v--;
edges.pb({u, v});
}
for (int h = 0; h <= 2000; h++) {
for (auto &x : edges) {
if (h < a[x.fi]) continue;
if (h >= a[x.se]) adj[enc(x.fi, h)].pb(enc(x.se, h));
if (h) {
if (h - 1 >= a[x.se]) adj[enc(x.fi, h)].pb(enc(x.se, h - 1));
if (h - 1 >= a[x.fi]) adj[enc(x.fi, h)].pb(enc(x.fi, h - 1));
}
if (h != 2000) {
adj[enc(x.fi, h)].pb(enc(x.se, h + 1));
adj[enc(x.fi, h)].pb(enc(x.fi, h + 1));
}
swap(x.fi, x.se);
if (h < a[x.fi]) {
swap(x.fi, x.se);
continue;
}
if (h >= a[x.se]) adj[enc(x.fi, h)].pb(enc(x.se, h));
if (h) {
if (h - 1 >= a[x.se]) adj[enc(x.fi, h)].pb(enc(x.se, h - 1));
if (h - 1 >= a[x.fi]) adj[enc(x.fi, h)].pb(enc(x.fi, h - 1));
}
if (h != 2000) {
adj[enc(x.fi, h)].pb(enc(x.se, h + 1));
adj[enc(x.fi, h)].pb(enc(x.fi, h + 1));
}
swap(x.fi, x.se);
}
}
queue<pii> bfs;
bfs.push({enc(0, 0), 0});
while (bfs.size()) {
auto cur = bfs.front();
bfs.pop();
int u = cur.fi; int dis = cur.se;
//if (dec(u).fi == n - 1) cout << dec(u).fi << " " << dec(u).se << "\n";
if (d[u] != -1) continue;
d[u] = dis;
for (auto &neighbour : adj[u]) {
if (d[neighbour] != -1) continue;
bfs.push({neighbour, dis + 1});
}
}
cout << d[enc(n - 1, 0)] << '\n';
}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
signed main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
//setIO();
int t = 1;
if (tc) {
cin >> t;
}
while (t--) {
solve();
}
}
Compilation message (stderr)
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:108:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:109:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |