1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstring> #include <deque> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> #define rep(i, l, r) for (int i = (l); i <= (r); ++i) #define per(i, l, r) for (int i = (l); i >= (r); --i) using std::cerr; using std::cin; using std::cout; using std::endl; using std::make_pair; using std::pair; typedef pair<int, int> pii; typedef long long ll; typedef unsigned int ui;
const int N = 100005;
std::vector<int> g[N], ng[N], fg[N]; int price[N];
int min[N], max[N], dfn[N], ins[N], low[N], color[N], dp[N]; std::stack<int> s; int cnt = 0; int T = 0;
void tarjan(int u) { dfn[u] = low[u] = ++T, ins[u] = 1; s.push(u); for (auto v : g[u]) { if (!dfn[v]) { tarjan(v); low[u] = std::min(low[u], low[v]); } else if (ins[v]) { low[u] = std::min(low[u], low[v]); } } if (dfn[u] == low[u]) { int x; cnt++; do { x = s.top(); s.pop(); color[x] = cnt, ins[x] = 0; min[cnt] = std::min(min[cnt], price[x]); max[cnt] = std::max(max[cnt], price[x]); } while (x != u); } }
int coutable[N];
void dfs(int u) { if (coutable[u]) return; coutable[u] = 1; for (auto v : fg[u]) dfs(v); }
int main() { #ifdef LOCAL freopen("input", "r", stdin); #endif std::memset(min, 0x3f, sizeof(min)); std::ios::sync_with_stdio(false); cout.tie(0); int n, m; cin >> n >> m; rep(i, 1, n) cin >> price[i]; rep(i, 1, m) { int x, y, z; cin >> x >> y >> z; g[x].push_back(y); if (z - 1) g[y].push_back(x); }
tarjan(1);
rep(u, 1, n) { for (auto v : g[u]) if (color[u] != color[v]) ng[color[u]].push_back(color[v]); }
rep(u, 1, cnt) for (auto v : ng[u]) fg[v].push_back(u); dfs(color[n]);
int ans = 0; std::memset(dp, 0x3f, sizeof(dp)); per(u, cnt, 1) { dp[u] = std::min(dp[u], min[u]); if (coutable[u]) ans = std::max(ans, max[u] - dp[u]); for (auto v : ng[u]) { dp[v] = std::min(dp[v], dp[u]); } }
cout << ans; return 0; }
|