# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1169841 | Thanhs | Airplane (NOI23_airplane) | C++20 | 191 ms | 25656 KiB |
#include <bits/stdc++.h>
using namespace std;
#define name "TENBAI"
#define fi first
#define se second
#define int long long
#define endl '\n'
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 2e5 + 5;
const int inf = 1e18;
vector<int> g[NM];
int n, m, a[NM], ans;
void calc(int s, vector<int>& d)
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
d.resize(n + 1, inf);
d[s] = 0;
pq.push({0, s});
while (pq.size())
{
int w = pq.top().fi, u = pq.top().se;
pq.pop();
if (d[u] != w)
continue;
for (auto v : g[u])
if (d[v] > max(d[u] + 1, a[v]))
{
d[v] = max(d[u] + 1, a[v]);
pq.push({d[v], v});
}
}
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> d1, d2;
calc(1, d1);
calc(n, d2);
ans = inf;
for (int i = 1; i <= n; i++)
{
setmin(ans, d1[i] + d2[i] + abs(d1[i] - d2[i]));
for (int j : g[i])
setmin(ans, d1[i] + d2[j] + abs(d1[i] - d2[j]) + 1);
}
cout << ans << endl;
}
signed main()
{
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
else if (fopen(name".inp", "r"))
{
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int tc = 1;
// cin >> tc;
while (tc--)
solve();
}
Compilation message (stderr)
# | 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... |