Topic
Enter the first line containing two integers n, M. n indicates the number of intersections, and M represents the number of road strips. Next m line, two integers per line, these two integers are between 1 and N, the first I+The two integers of line 1 represent the intersection number of the start and end points of the road of article I. Next n lines, one integer per line, in order to represent the amount of money in the ATM machine at each intersection. The next line contains two integers s, and p,s represents the city center number, which is the departure junction. P indicates the number of bars. The next line has a P-integer, which indicates that the number output of the intersection of P-Bars has an integer representing the maximum amount of cash that Banditji can rob from the center of the city to the end of a bar. Sample Input6 71 22 33 52 44 12 66 5Ten A8 -151 44356Sample Output -Tips -% of input guaranteed N, m<= the。 All inputs are guaranteed N, m<=500000。 The amount of money that is desirable in each ATM is a non-negative integer and does not exceed 4000. Enter data to ensure that you can reach at least one of the bars from the city centre along the one-way road of Siruseri.
View Code
This problem we need to use TARJAN+SPFA (to run the longest road)
The first thing to do is to run the point on the graph and Tarjan all the strong connected components, and set the parent node of the point on the strong connected component to the root of the strong connected component.
Because the points on the strongly connected component can reach the other points on the strong connected component as long as they can reach one, and a road can go many times.
The values of all the strongly connected components except the root are added to the root.
Then we re-map all the points that are not in the strongly connected component and the roots of all the strongly connected components as new points.
SPFA the longest road to a new diagram
Finally find out the parent nodes of all the bars and find out which of these nodes has the largest value from the starting point
Because the points on the strongly connected component can reach the other points on the strong connected component as long as they can reach one, and a road can go many times.
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;intCNT, hh[510000], hhh[510000], stack[510000];intdfn[510000], low[510000], num, ans, q, Top, w[510000];intfather[510000], dd[510000], J, N, H, T, l[510000], z, x, Y, M, S, p;BOOLd[510000];structnode{intV, next;}; Node b[510000], ss[510000];voidAddintCanintBB)//edge {b[++CNT].V =BB; B[cnt].next=HH[AA]; HH[AA]=CNT;}voidADDD (intCanintBB)//Connect side {ss[on new diagram++CNT].V =BB; Ss[cnt].next=HHH[AA]; HHH[AA]=CNT;}voidTarjan (intk) { inti; DFN[K]= Low[k] = + +num; stack[++top] =K; D[K]=true; for(i = hh[k]; I! =0; i =B[i].next) { intE =b[i].v; if(!Dfn[e]) {Tarjan (e); LOW[K]=min (low[k], low[e]); } Else if(D[e] = =true) {Low[k]=min (low[k], dfn[e]); } } if(Dfn[k] = =Low[k]) {D[k]=false; FATHER[K]=K; while(Stack[top]! =k) {W[k]+=W[stack[top]]; Father[stack[top]]=K; D[stack[top--]] =false; } Top--; }}voidrebuild () {CNT=0; inti; for(i =1; I <= N; i++) { for(j = hh[i]; J! =0; j =B[j].next) {T=b[j].v; if(Father[i] = = Father[t])Continue; ADDD (Father[i], father[t]); } }}voidSPFA () {inti; Q=s; memset (d,0,sizeof(d)); H=0, t =0; L[Q]=W[q]; D[Q]=true; while(1) { if(H > t) Break; for(i = hhh[q]; I! =0; i =Ss[i].next) {Z=ss[i].v; if(L[q] + w[z] >L[z]) {L[z]= L[q] +W[z]; if(D[z])Continue; dd[++T] =Z; D[Z]=true; }} D[q]=false; Q= dd[++h]; }}intMain () {inti; scanf ("%d%d", &n, &m); for(i =1; I <= m; i++) {scanf ("%d%d", &x, &y); Add (x, y); } for(i =1; I <= N; i++) {scanf ("%d", &W[i]); } scanf ("%d%d", &s, &p); for(j =1; J <= N; J + +) { if(!Dfn[j]) Tarjan (j); } rebuild ();//Use Tarjan to find all strongly connected components S=father[s];//If the starting point is in a strongly connected component, then the starting point is replaced by the root of the strongly connected component
Because the points on the strongly connected component can reach the other points on the strong connected component as long as they can reach one, and a road can go many times.
Important thing to say three times
SPFA ();//Use SPFA to find the longest road for(i =1; I <= p; i++) {scanf ("%d", &q); Ans=Max (ans, l[father[q]); } printf ("%d", ans); return 0;}
Bzoj 1179[APIO2009]ATM (TARJAN+SPFA)