0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-09 02:18:51 +00:00
OI-codes/Luogu/P3387/P3387.cpp

100 lines
1.9 KiB
C++
Raw Normal View History

2021-12-29 13:26:16 +00:00
#include <iostream>
2022-06-01 01:45:41 +00:00
#include <cstring>
2021-12-29 13:26:16 +00:00
#include <queue>
#include <stack>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
// Limits
const int N = 10005;
int n, m, u, v, a[N];
std::vector<int> g[N], g2[N];
// Tarjan
int cnt, dfn[N], low[N];
int scc_cnt, id[N];
int w[N];
bool vis[N];
std::stack<int> st;
void tarjan(int u) {
dfn[u] = low[u] = ++cnt;
vis[u] = true;
st.push(u);
for (int v : g[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = std::min(low[u], low[v]);
} else if (vis[v]) {
low[u] = std::min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
scc_cnt++;
int v;
do {
v = st.top();
st.pop();
vis[v] = false;
id[v] = scc_cnt;
w[scc_cnt] += a[v];
} while (v != u);
}
}
// Shortest Path
int dist[N];
2022-05-20 01:23:27 +00:00
2021-12-29 13:26:16 +00:00
int spfa(int s) {
memset(dist, -0x3f, sizeof(dist));
std::queue<int> q;
q.push(s);
dist[s] = 0;
int res = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
res = std::max(res, dist[u] + w[u]);
for (int v : g2[u]) {
if (dist[v] < dist[u] + w[u]) {
dist[v] = dist[u] + w[u];
q.push(v);
}
}
}
return res;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
cin >> u >> v;
g[u].push_back(v);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i);
}
}
for (int i = 1; i <= n; i++) {
for (int v : g[i]) {
if (id[i] != id[v]) {
g2[id[i]].push_back(id[v]);
}
}
}
int ans = 0;
for (int i = 1; i <= scc_cnt; i++) {
ans = std::max(ans, spfa(i));
}
cout << ans << endl;
return 0;
}