TLE #8,#9
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const ll MAXN = 5e5, MAXM = 5e5;
ll n, m, tot = 0, s, k, dfsTime = 0, sccCnt = 0, ans = 0;
ll u[MAXM + 5], v[MAXM + 5], head[MAXN + 5], a[MAXN + 5], pre[MAXN + 5], low[MAXN + 5], sccNo[MAXN + 5], money[MAXN + 5], rob[MAXN + 5];
struct Edge {
ll to, nxt;
} edge[MAXM + 5];
stack<ll> st;
bool vis[MAXN + 5];
inline void addEdge(ll u, ll v) {
edge[++tot].to = v;
edge[tot].nxt = head[u];
head[u] = tot;
}
void tarjan(ll u, ll father) {
st.push(u);
pre[u] = low[u] = ++dfsTime;
for (ll i = head[u]; i; i = edge[i].nxt) {
ll v = edge[i].to;
if (v == father) {
continue;
}
if (!pre[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
} else if (!sccNo[v]) {
low[u] = min(low[u], pre[v]);
}
}
if (pre[u] == low[u]) {
sccCnt++;
while (1) {
ll x = st.top();
st.pop();
sccNo[x] = sccCnt;
if (x == u) {
break;
}
}
}
}
void spfa(ll s) {
queue<ll> q;
q.push(s);
vis[s] = 1;
rob[s] = money[s];
while (!q.empty()) {
ll u = q.front();
q.pop();
vis[u] = 0;
for (ll i = head[u]; i; i = edge[i].nxt) {
ll v = edge[i].to, w = money[v];
if (rob[v] < rob[u] + w) {
rob[v] = rob[u] + w;
if (!vis[v]) {
q.push(v);
vis[v] = 1;
}
}
}
}
}
int main() {
scanf("%lld%lld", &n, &m);
for (ll i = 1; i <= m; i++) {
scanf("%lld%lld", &u[i], &v[i]);
addEdge(u[i], v[i]);
}
for (ll i = 1; i <= n; i++) {
scanf("%lld", &a[i]);
}
scanf("%lld%lld", &s, &k);
tarjan(s, 0);
tot = 0;
memset(head, 0, sizeof(head));
memset(edge, 0, sizeof(edge));
for (ll i = 1; i <= m; i++) {
if (sccNo[u[i]] != sccNo[v[i]]) {
addEdge(sccNo[u[i]], sccNo[v[i]]);
// addEdge(sccNo[v[i]], sccNo[u[i]]);
}
}
for (ll i = 1; i <= n; i++) {
money[sccNo[i]] += a[i];
}
spfa(sccNo[s]);
while (k--) {
ll p;
scanf("%lld", &p);
ans = max(ans, rob[sccNo[p]]);
}
printf("%lld\n", ans);
return 0;
}
我太菜了,看不出来哪里有问题,求助!(好像别人用spfa都不会T)……