Files
cpp-courseware/2.Course/2.深入浅出程序设计竞赛(提高篇)/最短路/C++源码/07_04_p1073.cpp
T

43 lines
1.5 KiB
C++

// P1073 最优贸易。价格是 1..100,单调标签至多改善 100 次。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m; if (!(cin >> n >> m)) return 0;
vector<int> price(n + 1);
for (int i = 1; i <= n; ++i) cin >> price[i];
vector<vector<int>> g(n + 1), rg(n + 1);
auto add = [&](int u, int v) { g[u].push_back(v); rg[v].push_back(u); };
for (int i = 0, u, v, z; i < m; ++i) {
cin >> u >> v >> z; add(u,v); if (z == 2) add(v,u);
}
vector<int> low(n + 1, 101), high(n + 1, -1);
vector<bool> inq(n + 1, false);
queue<int> q; low[1] = price[1]; q.push(1); inq[1] = true;
while (!q.empty()) {
int u = q.front(); q.pop(); inq[u] = false;
for (int v : g[u]) {
int cand = min(low[u], price[v]);
if (cand < low[v]) {
low[v] = cand;
if (!inq[v]) { q.push(v); inq[v] = true; }
}
}
}
high[n] = price[n]; q.push(n); inq[n] = true;
while (!q.empty()) {
int u = q.front(); q.pop(); inq[u] = false;
for (int v : rg[u]) {
int cand = max(high[u], price[v]);
if (cand > high[v]) {
high[v] = cand;
if (!inq[v]) { q.push(v); inq[v] = true; }
}
}
}
int ans = 0;
for (int v = 1; v <= n; ++v)
if (low[v] <= 100 && high[v] >= 1) ans = max(ans, high[v]-low[v]);
cout << ans << '\n';
}