给一个有向图,每个点有价值,第一次到达这个点可以获得他的价值,给出起点和若干个终点,问最大能获得的价值是多少
缩点后变成一个DAG,f[i]表示起点到这个点最大价值,直接拓扑序DP就好了
code:
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<cmath>
#include<ctime>
#include<cmath>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
inline void read(int &x)
{
char c;
while(!((c=getchar())>='0'&&c<='9'));
x=c-'0';
while((c=getchar())>='0'&&c<='9') (x*=10)+=c-'0';
}
inline void up(int &x,const int &y){if(x<y)x=y;}
inline void down(int &x,const int &y){if(x>y)x=y;}
const int maxn = 1100000;
const int maxm = 1100000;
int n,m,st;
int s[maxn];
int e[maxm][2];
struct edge
{
int y,nex;
edge(){}
edge(const int &_y,const int &_nex){y=_y;nex=_nex;}
}a[maxm<<1]; int len,fir[maxn];
inline void ins(const int x,const int y){a[++len]=edge(y,fir[x]); fir[x]=len;}
bool v[maxn];
int t[maxn],tp;
int dfn[maxn],low[maxn],id;
int bel[maxn],cnt,fx[maxn];
void tarjan(const int x)
{
t[++tp]=x; v[x]=true;
dfn[x]=low[x]=++id;
for(int k=fir[x];k;k=a[k].nex)
{
const int y=a[k].y;
if(!dfn[y]) tarjan(y),down(low[x],low[y]);
else if(v[y]) down(low[x],dfn[y]);
}
if(low[x]==dfn[x])
{
int la=0; fx[++cnt]=0;
while(la!=x)
{
v[la=t[tp--]]=false;
bel[la]=cnt;
fx[cnt]+=s[la];
}
}
}
queue<int>q;
void bfs()
{
q.push(st); v[st]=true;
while(!q.empty())
{
const int x=q.front(); q.pop();
for(int k=fir[x];k;k=a[k].nex)
{
const int y=a[k].y;
if(!v[y]) v[y]=true,q.push(y);
}
}
}
int _in[maxn];
void rebuild()
{
tarjan(st);
len=0; for(int i=1;i<=n;i++) fir[i]=0;
for(int i=1;i<=m;i++)
{
const int x=e[i][0],y=e[i][1];
if(bel[x]&&bel[y]&&bel[x]!=bel[y]) ins(bel[x],bel[y]),_in[bel[y]]++;
}
}
int f[maxn],P[maxn],pn;
int solve()
{
rebuild();
q.push(bel[st]); f[bel[st]]=fx[bel[st]];
while(!q.empty())
{
const int x=q.front(); q.pop();
for(int k=fir[x];k;k=a[k].nex)
{
const int y=a[k].y;
_in[y]--;
if(!_in[y]) q.push(y);
up(f[y],f[x]+fx[y]);
}
}
int ans=0;
for(int i=1;i<=pn;i++) up(ans,f[bel[P[i]]]);
return ans;
}
int main()
{
len=0; //
read(n); read(m);
for(int i=1;i<=m;i++)
{
int x,y; read(x); read(y);
ins(x,y);
e[i][0]=x; e[i][1]=y;
}
for(int i=1;i<=n;i++) read(s[i]);
read(st); read(pn);
for(int i=1;i<=pn;i++) read(P[i]);
printf("%d\n",solve());
return 0;
}