标签:ref min void tar col span pop while spfa
一道\(Tarjan\)缩点水题。因为可以反复经过节点,所以把一个联通快中的所有路口看做一个整体,缩点后直接跑spfa或者dp就好了。
我选择了在DAG上跑dp,毕竟复杂度\(O(n)\)
拓扑时搞DP,\(f[i]\)表示在DAG上\(i\)节点时,当前最大钱数,转移\(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;
}
标签:ref min void tar col span pop while spfa
原文地址:https://www.cnblogs.com/santiego/p/10943425.html