#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(),x.end()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
int n,m,a[maxn],l[maxn],r[maxn],maxl[maxn],maxr[maxn];
vi ke[maxn];
void dij(int inp,int d[],int g[],int n)
{
fo(i,1,n) d[i] = inf,g[i] = 0;
d[inp] = 0;
priority_queue<pii,vii,greater<pii>> q;
q.push({0,inp});
while(q.size())
{
auto [du,u] = q.top();
q.pop();
if(d[u] != du) continue;
for(int v : ke[u])
{
int val = max(1LL,a[v] - a[u]);
if(minimize(d[v],du + val))
{
d[v] = d[u] + val;
g[v] = max(a[v],g[u]);
q.push({d[v],v});
}
else if(d[v] == du + val)
{
maximize(g[v],max(a[v],g[u]));
}
}
}
}
pii edge[maxn];
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
fo(i,1,n) cin >> a[i];
fo(i,1,m)
{
int u,v;
cin >> u >> v;
ke[u].pb(v);
ke[v].pb(u);
edge[i] = {u,v};
}
dij(1,l,maxl,n);
dij(n,r,maxr,n);
int ans = inf;
// fo(i,1,n) cout << l[i] << ' ' << r[i] << ' ' << maxl[i] << ' ' << maxr[i] << "\n";
fo(i,1,m)
{
auto [u,v] = edge[i];
minimize(ans,l[u] + r[v] + max(1LL,abs(maxr[v] - maxl[u])));
minimize(ans,r[u] + l[v] + max(1LL,abs(maxr[u] - maxl[v])));
}
// cout << l[2] << ' '<< r[3] << ' ' << maxr[3];en;
cout << ans;
}
Compilation message
Main.cpp:73:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
73 | main()
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:78:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33272 KB |
Output is correct |
2 |
Incorrect |
52 ms |
57196 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33116 KB |
Output is correct |
3 |
Correct |
7 ms |
33116 KB |
Output is correct |
4 |
Correct |
7 ms |
33112 KB |
Output is correct |
5 |
Correct |
5 ms |
33116 KB |
Output is correct |
6 |
Incorrect |
6 ms |
33372 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33116 KB |
Output is correct |
2 |
Correct |
5 ms |
33116 KB |
Output is correct |
3 |
Correct |
7 ms |
33116 KB |
Output is correct |
4 |
Correct |
7 ms |
33112 KB |
Output is correct |
5 |
Correct |
5 ms |
33116 KB |
Output is correct |
6 |
Incorrect |
6 ms |
33372 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33272 KB |
Output is correct |
2 |
Incorrect |
52 ms |
57196 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |