首先比较容易发现一个性质:
如果可以进入一个强连通分量,那么这个强连通分量内的所有点就都可以被走到。
于是我们就可以考虑对整张图进行 Tarjan 缩点,每一个强连通分量的点权为这个强连通分量内部所有点的点权,这样整张图就变成了一个 DAG。
那么用 SPFA 跑一遍最长路,最后直接对 (dis[)每个酒吧所在的强连通分量编号(]) 取一个 (max) 即为答案。
注意实现上的一些细节。
#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d
", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
typedef pair <int, PII> PIII;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
inline LL gl()
{
LL f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f, N = 1000003, M = N << 1;
int n, m;
int dfn[N], low[N], tim, stk[N], topp, scc_cnt, sz[N], id[N];
bool in_stk[N];
int tot, head[N], headc[N], ver[M], nxt[M], edge[N];
int a[N];
int S, P;
int bar[N];
inline void add(int u, int v)
{
ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;
}
inline void add1(int u, int v, int w)
{
ver[++tot] = v, edge[tot] = w, nxt[tot] = headc[u], headc[u] = tot;
}
void Tarjan(int u)
{
dfn[u] = low[u] = ++tim, stk[++topp] = u, in_stk[u] = true;
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (!dfn[v])
{
Tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (in_stk[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u])
{
int y = -1;
++scc_cnt;
do
{
y = stk[topp--];
in_stk[y] = false;
sz[scc_cnt] += a[y];
id[y] = scc_cnt;
} while (y != u);
}
}
int dis[N];
bool vis[N];
inline void SPFA(int s)
{
dis[s] = sz[s];
vis[s] = true;
queue <int> q;
q.push(s);
while (!q.empty())
{
int u = q.front(); q.pop(); vis[u] = false;
for (int i = headc[u]; i; i = nxt[i])
{
int v = ver[i], w = edge[i];
if (dis[v] < dis[u] + w)
{
dis[v] = dis[u] + w;
if (!vis[v])
vis[v] = true, q.push(v);
}
}
}
}
int main()
{
n = gi(), m = gi();
for (int i = 1; i <= m; i+=1)
{
int u = gi(), v = gi();
add(u, v);
}
for (int i = 1; i <= n; i+=1) a[i] = gi();
S = gi(), P = gi();
for (int i = 1; i <= P; i+=1) bar[i] = gi();
for (int i = 1; i <= n; i+=1) if (!dfn[i]) Tarjan(i);
for (int u = 1; u <= n; u+=1)
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (id[u] != id[v])
add1(id[u], id[v], sz[id[v]]);
}
SPFA(id[S]);
int ans = 0;
for (int i = 1; i <= P; i+=1) ans = max(ans, dis[id[bar[i]]]);
printf("%d
", ans);
return 0;
}