嘛,考虑,图的大小以及强连通分量的性质,
可以轻易推出本题做法
#include<bits/stdc++.h> #define MAXN 500005 using namespace std; int n,m,tot,tot2,dx,tx,maxl; int s,p,ed; int h[MAXN],h2[MAXN],low[MAXN],dfn[MAXN],belong[MAXN]; int a[MAXN],sum[MAXN],f[MAXN]; bool vis[MAXN]; stack<int>q; struct node{ int from,to,next; }e[MAXN<<1],e2[MAXN]; void init(){ tot = tot2 = dx = tx = maxl = 0; memset(h,-1,sizeof(h)); memset(h2,-1,sizeof(h2)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(f,0,sizeof(f)); memset(sum,0,sizeof(sum)); memset(a,0,sizeof(a)); memset(vis,false,sizeof(vis)); } void add(int x,int y){ tot++; e[tot].from = x; e[tot].to = y; e[tot].next = h[x]; h[x] = tot; } void add2(int x,int y){ tot2++; e2[tot2].from = x; e2[tot2].to = y; e2[tot2].next = h2[x]; h2[x] = tot2; } int tarjan(int now){ dx++; low[now] = dfn[now] = dx; q.push(now),vis[now] = true; for(int i = h[now];i != (-1);i = e[i].next){ if(!dfn[e[i].to]){ tarjan(e[i].to); low[now] = min(low[now] , low[e[i].to]); } else if(vis[e[i].to]){ low[now] = min(low[now] , dfn[e[i].to]); } } if(dfn[now]==low[now]){ int u; tx++; do{ u = q.top(); belong[u] = tx; sum[tx]+=a[u]; q.pop(); vis[u] = false; }while(u!=now); } } void solve(){ queue<int>t; t.push(belong[s]); f[belong[s]]=sum[belong[s]]; while(!t.empty()){ int u = t.front();t.pop(); for(int i = h2[u];i!=(-1);i=e2[i].next){ if(f[e2[i].to]<f[u]+sum[e2[i].to]){ f[e2[i].to]=f[u]+sum[e2[i].to]; t.push(e2[i].to); } } } } int main(){ while(scanf("%d%d",&n,&m)==2){ init(); for(int i=1;i<=m;i++){ int x,y; scanf("%d%d",&x,&y); add(x,y); } for(int i=1;i<=n;i++)scanf("%d",&a[i]); scanf("%d%d",&s,&p); tarjan(s); for(int i=1;i<=m;i++){ if(belong[e[i].from] == belong[e[i].to])continue; add2(belong[e[i].from] , belong[e[i].to]); } solve(); for(int i=1;i<=p;i++){ scanf("%d",&ed); maxl = max(maxl,f[belong[ed]]); } cout<<maxl<<endl; } }
原文:https://www.cnblogs.com/shatianming/p/12328942.html