First, the algorithm introduction
tarjan--to solve the strongly connected component of the graph. This algorithm in my blog has introduced, here will not repeat. Post the blog link to introduce Tarjan: http://www.cnblogs.com/Maki-Nishikino/p/5866191.html
Then let's talk about SPFA:
SPFA Full Name shortest Path Faster algorithm for solving single source shortest circuit. Since there is "Faster" in the name, there must be something extraordinary about it, and indeed it is more efficient than Dijkstra and Bellman-ford.
The idea is roughly as follows:
1, first use the adjacency table to survival down, and specify an array d,d[i] to indicate the shortest distance from the starting point to I;
2, set up a queue, the starting point into the queue;
3, the first element of the team to perform a relaxation operation, traverse all the team first element as the starting point of the edge, if the traversed edge can make to the end of the traversed edge of the path shorter, then update the shortest path, and the end of the traversed edge of the team end;
4, each to complete a relaxation, make the team first element out of the team, repeat 2, until the queue has no elements.
Forgive bloggers lazy to stick pseudo-code, I will directly talk about the problem, anyway, there are templates # manual funny
Second, APIO2009 ATM
Original title link (from Bzoj): http://www.lydsy.com/JudgeOnline/problem.php?id=1179
Title Description:
Input:
The first line consists of 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, and the two integers of line i+1 represent the intersection of the starting and ending points of the road of article I
Number. 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. Next
Has a P-integer in the row, indicating the number of the intersection where the bar is p.
Output:
Output An integer that represents the maximum amount of cash that Banditji can rob from the center of the city to the end of a bar.
Sample input:
6 7
1 2
2 3
3 5
2 4
4 1
2 6
6 5
10
12
8
16
1
5
1 4
4
3
5
6
Sample output:
47
Data range:
50% input guaranteed N, m<=3000. 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.
For this problem, we consider the first to use Tarjan to find all its strong connected components, and then the same boingonium a strong connected component of the ATM money together, so that a strong connected component on the point of contraction into a point. Then take the city center s as the starting point, using SPFA to run out of S to other points of the longest (most valuable) road, compare the bar where the point of D value, output large can.
Attached code:
1#include <stdio.h>2#include <algorithm>3#include <string.h>4 using namespacestd;5 structnode6 {7 intv;8 intNext;9 intW;Ten }; One intn,m; ANode e[500010],map[500010];//adjacency Table Store map - intst[500010],head[500010],cnt; - intatm[500010],money[500010]; the intd[500010],q[500010];//Shortest Path &SPFA the queue to use - voidBuildintAintb) - { -e[++cnt].v=b; +e[cnt].next=St[a]; -st[a]=CNT; +}//Building graphs to find strong connected components A intstack[500010],top;//Tarjan Required Stacks at intdfn[500010],low[500010],dex;//Timestamp (Deep search), time stamp in the earliest stack that can be traced back, sequence number - BOOLvis[500010];//Tarjan to determine if the point is in the stack, SPFA when the point is in the queue - intcolor[500010],num;//represents a point on the same strong connected component - voidTarjan (intX//Tarjan Finding strong connected components - { -dfn[x]=++Dex; inlow[x]=Dex; -vis[x]=true; toStack[++top]=x;//current point in stack + inti; - for(i=st[x];i!=0; i=e[i].next)//enumerates edges starting at the current point the { * intTEMP=E[I].V;//temp is the end point of the currently enumerated edge $ if(!dfn[temp])//If the current edge endpoint is not processedPanax Notoginseng { - Tarjan (temp); thelow[x]=min (low[x],low[temp]); + } A Else if(Vis[temp]) low[x]=min (low[x],dfn[temp]); the } + if(dfn[x]==Low[x]) - { $vis[x]=false; $Color[x]=++num;//Mark points within the current strongly connected component - while(stack[top]!=x)//stack top elements sequentially out of stack - { thecolor[stack[top]]=num; -vis[stack[top--]]=false;Wuyi } thetop--; - } Wu } - voidAdd ()//The points on the same strong connected component are shrunk to a point, and these points are connected to a new picture About { $Cnt=0; - inti,j; - for(i=1; i<=n;i++) - { A for(j=st[i];j!=0; j=e[j].next) + { the inttemp=e[j].v; - if(color[i]!=Color[temp]) $ { themap[++cnt].v=Color[temp]; themap[cnt].next=Head[color[i]]; thehead[color[i]]=CNT; the } - } in the } the } About voidSPFA (intX//SPFA Find the longest way the { thememset (Vis,false,sizeof(Vis)); the intL=1, r=1; +Q[l]=x;//The initial point is placed in the queue -vis[x]=true; thed[x]=Money[x];Bayi while(l<=R) the { the intu=q[l++]; - for(inti=head[u];i!=0; i=map[i].next)//traverse all edges starting at the current point - { the intv=map[i].v; the if(d[v]<d[u]+Money[v]) the { thed[v]=d[u]+Money[v]; - if(Vis[v])Continue; theQ[++r]=v;//If the end of the currently expanding edge is not in the queue, put it in the tail of the team thevis[v]=true; the }94 } thevis[u]=false; the } the }98 intMain () About { - inta,b,i,s,p,o,ans=0;101scanf"%d%d",&n,&m);102 for(i=1; i<=m;i++)103 {104scanf"%d%d",&a,&b); the Build (A, b);106}//build the initial diagram107 for(i=1; i<=n;i++)108 {109 if(!dfn[i]) Tarjan (i);//finding strong connected components the }111Add ();//build a new map the for(i=1; i<=n;i++)113 { thescanf"%d",&atm[i]); themoney[color[i]]+=Atm[i]; the }117scanf"%d%d",&s,&p);118SPFA (Color[s]);//find single source shortest way119 for(i=1; i<=p;i++) - {121scanf"%d",&o);122Ans=max (Ans,d[color[o]);//find the longest way to the end of a bar123 }124printf"%d", ans); the return 0;126}
APIO2009 Atm
"Tarjan" + "SPFA" "APIO2009" Atm