觉得还是把其他记录一起放上来比较好。。。
tarjan缩圈后求最短路即可。。速度好渣= =
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 1e9 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 1000000+5 10 using namespace std; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} 14 while(isdigit(ch))x=x*10+ch-'0',ch=getchar(); 15 return x*f; 16 } 17 struct edge{ 18 int t; 19 edge *next; 20 }e[NM],*h[NM],*o=e; 21 void add(int x,int y){ 22 o->t=y;o->next=h[x];h[x]=o;o++; 23 } 24 int n,m,_x,_y,d[NM],suc[NM],low[NM],cnt,tot,a[NM]; 25 bool v[NM]; 26 queue<int >q; 27 stack<int >s; 28 void dfs(int x){ 29 d[x]=low[x]=++tot;s.push(x); 30 link(x) 31 if(!d[j->t]){ 32 dfs(j->t); 33 low[x]=min(low[x],low[j->t]); 34 }else if(!suc[j->t]) 35 low[x]=min(low[x],low[j->t]); 36 if(d[x]==low[x]){ 37 int t;cnt++; 38 do{ 39 t=s.top();s.pop(); 40 suc[t]=cnt+n; 41 }while(x!=t); 42 } 43 } 44 int spfa(){ 45 v[_x]++;d[_x]=a[_x];q.push(_x); 46 while(!q.empty()){ 47 int t=q.front();q.pop();v[t]=false; 48 link(t) 49 if(d[j->t]<d[t]+a[j->t]){ 50 d[j->t]=d[t]+a[j->t]; 51 if(!v[j->t])v[j->t]++,q.push(j->t); 52 } 53 } 54 return d[_y]; 55 } 56 int main(){ 57 // freopen("data.in","r",stdin); 58 n=read();m=read(); 59 inc(i,1,m){ 60 _x=read();_y=read(); 61 add(_x,_y); 62 } 63 inc(i,1,n) 64 if(!d[i])dfs(i); 65 inc(i,1,n)a[i]=read(); 66 _x=read();m=read();_x=suc[_x];_y=cnt+n+1; 67 inc(i,1,m) 68 add(suc[read()],_y); 69 inc(i,n,cnt+n){ 70 inc(k,1,n) 71 if(suc[k]==i){ 72 a[i]+=a[k]; 73 link(k) 74 if(suc[j->t]!=i)add(i,suc[j->t]); 75 } 76 } 77 printf("%d ",spfa()); 78 return 0; 79 }