Test instructions: There is a n*m of ore, each with a mine, or a portal, or a rock in the way. In addition to the rock, other squares can go right or down and walk to a non-rocky lattice. For each occurrence, it will be able to get all of its ore, and for each portal you can choose to transmit or not transmit, continue to the right or downward (portal delivery point may also be Rock), from top to bottom, from left to right in order for each portal given a delivery point. How much ore can be obtained by asking.
For such a diagram, we can find that there are some points, due to the existence of portals, must be able to reach each other, then these points can be strongly connected to the contraction point, and then for the undirected graph can easily be used to memorize the maximum value of the search.
1#include <stdio.h>2#include <string.h>3#include <stack>4#include <queue>5 using namespacestd;6 7 Const intmaxn=1605;8 Const intmaxm=1e5;9 Ten Chars[ $][ $]; One inthead[2][maxn],point[2][maxm],nxt[2][maxm],size[2]; A intn,t,scccnt; - intSTX[MAXN],LOW[MAXN],SCC[MAXN],NUM[MAXN],V[MAXN]; - intDP[MAXN]; thestack<int>S; - - intMaxintAintb) {returnA>b?a:b;} - + voidinit () { -memset (head,-1,sizeof(head)); +size[0]=size[1]=0; Amemset (NUM,0,sizeof(num)); atmemset (dp,-1,sizeof(DP)); - } - - voidAddintAintBintC=0){ -point[c][size[c]]=b; -nxt[c][size[c]]=Head[c][a]; inhead[c][a]=size[c]++; - } to + voidDfsints) { -stx[s]=low[s]=++T; the S.push (S); * for(inti=head[0][s];~i;i=nxt[0][i]) { $ intj=point[0][i];Panax Notoginseng if(!Stx[j]) { - Dfs (j); thelow[s]=min (low[s],low[j]); + } A Else if(!Scc[j]) { thelow[s]=min (low[s],stx[j]); + } - } $ if(low[s]==Stx[s]) { $scccnt++; - while(1){ - intu=S.top (); S.pop (); thescc[u]=scccnt; -num[scccnt]+=V[u];Wuyi if(S==u) Break; the } - } Wu } - About voidSETSCC () { $memset (STX,0,sizeof(STX)); -memset (SCC,0,sizeof(SCC)); -T=scccnt=0; - for(intI=1; i<=n;++i)if(!Stx[i]) DFS (i); A for(intI=1; i<=n;++i) { + for(intj=head[0][i];~j;j=nxt[0][j]) { the intk=point[0][j]; - if(scc[i]!=Scc[k]) { $Add (Scc[i],scc[k],1); the } the } the } the } - in intDp (ints) { the if(~dp[s])returnDp[s]; the intmaxx=0; About for(inti=head[1][s];~i;i=nxt[1][i]) { theMaxx=max (MAXX,DP (point[1][i])); the } the returndp[s]=num[s]+Maxx; + } - the intMain () {Bayi intT; thescanf"%d",&T); the while(t--){ - intm,l; -scanf"%d%d",&l,&m); then=m*l; the init (); the for(intI=1; i<=l;++i) scanf ("%s", s[i]+1); the intb; - for(intI=1; i<=l;++i) { the for(intj=1; j<=m;++j) { the intp= (I-1) *m+J; the if(s[i][j]>='0'&&s[i][j]<='9'){94v[p]=s[i][j]-'0'; the } the Elsev[p]=0; the if(s[i][j]!='#'){98 if(i+1<=l&&s[i+1][j]!='#'){ AboutAdd (p,p+m); - }101 if(j+1<=m&&s[i][j+1]!='#'){102Add (p,p+1);103 }104 } the if(s[i][j]=='*'){106scanf"%d%d",&a,&b);107a++;108b++;109 if(s[a][b]!='#'){ the intP1= (A-1) *m+b;111 Add (P,P1); the }113 } the } the } the SETSCC ();117printf"%d\n", Dp (scc[1]));118 }119 return 0; -}
View Code
poj3592 strong connectivity + Memory search