#include <bits/stdc++.h>
#define task "BriantheCrab"
#define int long long
#define pii pair <int, int>
#define fi first
#define se second
#define szf sizeof
#define sz(s) (int)((s).size())
using namespace std;
template <class T> void mini (T &t, T f) {if (t > f) t = f;}
template <class T> void maxi (T &t, T f) {if (t < f) t = f;}
const int maxN = 1e5 + 5;
const int inf = 1e18 + 7;
const int mod = 1e9 + 7;
int n, m;
int a[maxN];
vector <int> adj[maxN];
namespace sub1 {
int checker = 0;
void sol () {
int sum = 0;
for (int i = 1; i <= n; i ++) {
sum += (abs (a[i] - a[i + 1]));
}
cout << sum;
return;
}
}
namespace sub2 {
const int N2 = 2e3 + 5;
struct Edge {
int node, dis, hi;
};
struct cmp {
bool operator () (Edge A, Edge B) {
return A.dis > B.dis;
}
};
int L[N2][N2];
int limH = 0;
int cost (int h1, int h2) {
if (abs (h1 - h2) <= 1) {
return 0;
}
return abs (h1 - h2) - 1;
}
void dijkstra () {
for (int i = 0; i <= n; i ++) {
for (int j = 0; j <= limH; j ++) {
L[i][j] = inf;
}
}
priority_queue <Edge, vector <Edge>, cmp> pq;
L[1][0] = 0;
pq.push ({1, L[1][0], 0});
while (!pq.empty ()) {
auto [u, c, h] = pq.top ();
//cout << u << ' ' << c << ' ' << h << '\n';
pq.pop ();
if (L[u][h] > c) {
continue;
}
for (auto v : adj[u]) {
for (int nh = a[v]; nh <= limH; nh ++) {
if (L[v][nh] > L[u][h] + 1 + cost (nh, h)) {
L[v][nh] = L[u][h] + 1 + cost (nh, h);
pq.push ({v, L[v][nh], nh});
}
}
}
}
}
void sol () {
limH = *max_element (a + 1, a + n + 1);
dijkstra ();
cout << L[n][0];
}
}
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;
adj[u].push_back (v);
adj[v].push_back (u);
if (u == i && v == i + 1) {
sub1 :: checker ++;
}
}
if (sub1 :: checker == n - 1) {
sub1 :: sol ();
}
else if (n <= 2000 && *max_element (a + 1, a + n + 1) <= 2000) {
sub2 :: sol ();
}
}
signed main () {
cin.tie (nullptr) -> sync_with_stdio (false);
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int t = 1;
//cin >> t;
while (t --) {
solve ();
}
return 0;
}
// thfdgb
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:121:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:122:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen (task".out", "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... |