APIO2009 looting plan Tarjan DAG-DP
A \ (Tarjan \) shrink a little water problem. Because it can be repeated through node, to all junctions of a fast link seen as a whole, run directly or after condensing point dp spfa like.
I chose to run dp on DAG, after all, the complexity of the \ (O (n) \)
Engage DP topology, \ (F [I] \) represented on the DAG \ (I \) node, the current maximum number of money transfer \ (f [v] = max (f [v], f [u] + w [v]) \)
#include <cstdio>
#include <queue>
#define MAXN 500005
#define MIN(A,B) ((A)<(B)?(A):(B))
#define MAX(A,B) ((A)>(B)?(A):(B))
using namespace std;
int n,m,sta,p;
bool hav[MAXN],col_hav[MAXN];
int head[MAXN],nxt[MAXN],vv[MAXN],tot;
inline void add_edge(int u, int v){
vv[++tot]=v;
nxt[tot]=head[u];
head[u]=tot;
}
int s[MAXN],top;
bool ins[MAXN];
int col[MAXN],col_cnt;
int val[MAXN],col_val[MAXN];
int low[MAXN],dfn[MAXN],cnt;
void tarjan(int u){
dfn[u]=++cnt;
low[u]=cnt;
s[++top]=u;
ins[u]=1;
for(register int i=head[u];i;i=nxt[i]){
int v=vv[i];
if(dfn[v]==0){
tarjan(v);
low[u]=MIN(low[u], low[v]);
}else if(ins[v]){
low[u]=MIN(low[u], dfn[v]);
}
}
if(dfn[u]==low[u]){
col[u]=++col_cnt;
ins[u]=0;
col_val[col_cnt]=val[u];
while(s[top]!=u){
col[s[top]]=col_cnt;
ins[s[top]]=0;
col_val[col_cnt]+=val[s[top]];
top--;
}
top--;
}
}
int head2[MAXN],nxt2[MAXN],vv2[MAXN],tot2;
inline void add_edge2(int u, int v){
vv2[++tot2]=v;
nxt2[tot2]=head2[u];
head2[u]=tot2;
}
int rdu[MAXN];
inline void build(){
for(register int u=1;u<=n;++u)
if(dfn[u]!=0){
for(register int i=head[u];i;i=nxt[i]){
int v=vv[i];
if(col[u]==col[v]) continue;
rdu[col[v]]++;
add_edge2(col[u], col[v]);
}
}
}
queue <int> q;
int f[MAXN],ans=0;
void dp(){
q.push(col[sta]);f[col[sta]]=col_val[col[sta]];
while(!q.empty()){
int u=q.front();q.pop();
for(register int i=head2[u];i;i=nxt2[i]){
int v=vv2[i];
f[v]=MAX(f[v], f[u]+col_val[v]);
if((--rdu[v])==0) q.push(v);
}
}
for(register int i=1;i<=col_cnt;++i)
if(col_hav[i]) ans=MAX(f[i], ans);
}
int main()
{
scanf("%d %d", &n, &m);
while(m--){
int a,b;scanf("%d %d", &a, &b);
add_edge(a,b);
}
for(register int i=1;i<=n;++i) scanf("%d", &val[i]);
scanf("%d %d", &sta, &p);
while(p--){
int x;scanf("%d", &x);
hav[x]=1;
}
tarjan(sta);
for(register int i=1;i<=n;++i)
if(hav[i]) col_hav[col[i]]=1;
build();
dp();
printf("%d", ans);
return 0;
}