BZOJ2702[SDOI2012] Go Maze

Source: Internet
Author: User

Test instructions: give you a graph, point 10000, the number of sides 1000000,SCC size not more than 100 (according to the data range of the third part of the data to meet this condition, but the second part of the data does not appear in the size of more than 100 points of the SCC, I was using the array size of 100 code to defy the 2333) to go randomly from s, ask to walk to the desired step of T.

First consider the INF case. If you go from S to a point where you can't go to T, such as this data: The red point is the starting point and the green point is the end, then there is a 1/2 probability that you will never go (stop at the blue dot).

Note that the occurrence of the ring is not necessarily the INF, because the probability of walking infinity on the ring may be infinitesimal. Therefore, the first point, the side of the reverse to find all the SCC can not reach the T, if it is possible to get from S to such an SCC or s itself in such an SCC, then the answer is INF.

Next, we turn the desired step into the desired number of points (obviously through the number of edges equal to 1), then using the desired linearity, only Gaussian elimination to find the desired number of each point and then add up. But this range obviously cannot be done directly. The SCC size is less than 100, which reminds us that we can do Gaussian elimination for each SCC, and then consider the relationship between the SCC. The idea is similar to Usaco a shortest problem "road and Route", the problem is to each SCC run Dijkstra.

The specific approach: f[i] is the desired number of times for point I, G[i] is the desired count of going from one SCC to the other, because we process each SCC in topological order, so that each SCC in this SCC is processed at each point in the g[] The values have been calculated. Next, one equation is listed for each point in the SCC. For Point X,f[x]=g[x]+sigma (F[j]/outdeg[j]), J to X has a forward edge and J and X are in the same scc,outdeg. Here j can be equal to X (with self-loop), to verify that this time the equation is also right. After the SCC is finished, update the g[of other SCC with the point in this SCC]. Note the boundary g[s]=1,f[t]=1

Then the code code will be fine.

#include <cstdio>#include<vector>#include<algorithm>using namespacestd;intn,m,s,t;Const intmaxn=10005, maxm=1000005;//Graph theorystructedge{intTo,next;} LST1[MAXM],LST2[MAXM],LST3[MAXM];intlen1=1, len2=1, len3=1;intFIRST1[MAXN],FIRST2[MAXN],FIRST3[MAXN];voidAddedge1 (intAintb) {lst1[len1].to=b;lst1[len1].next=First1[a]; First1[a]=len1++;}voidAddedge2 (intAintb) {lst2[len2].to=b;lst2[len2].next=First2[a]; First2[a]=len2++;}voidAddedge3 (intAintb) {lst3[len3].to=b;lst3[len3].next=First3[a]; First3[a]=len3++;}intOUTDEG[MAXN];intBelong[maxn],tot,sz[maxn];vector<int>SCC[MAXN];intstk[maxn],top,dfn[maxn],low[maxn],t;BOOLINS[MAXN];namespacetrajan{voidDfsintx) {Low[x]=dfn[x]=++T; Stk[top++]=x;ins[x]=true;  for(intPT=FIRST1[X];p t;pt=Lst1[pt].next) {      if(!Dfn[lst1[pt].to])    {DFS (lst1[pt].to); if(Low[lst1[pt].to]<low[x]) low[x]=Low[lst1[pt].to]; }Else if(Ins[lst1[pt].to]&&dfn[lst1[pt].to]<low[x]) low[x]=Dfn[lst1[pt].to]; }    if(dfn[x]==Low[x]) {      ++tot;  Do{ins[stk[--top]]=false; Belong[stk[top]]=tot;    Scc[tot].push_back (Stk[top]); Sz[tot]++; } while(stk[top]!=x); }  }  voidTarjan () { for(intI=1; i<=n;++i) {      if(!Dfn[i]) DFS (i); }     for(intI=1; i<=n;++i) {       for(intPt=first1[i];p t;pt=Lst1[pt].next) {    if(belong[lst1[pt].to]!=Belong[i])      {addedge2 (belong[lst1[pt].to],belong[i]);    Addedge3 (belong[i],belong[lst1[pt].to]); }      }    }  }  BOOLREACHFROMEND[MAXN],MUSTREACHEND[MAXN]; voidPredfs (intx) {Reachfromend[x]=true;  for(intPT=FIRST2[X];p t;pt=Lst2[pt].next) {      if(!Reachfromend[lst2[pt].to])      {Predfs (lst2[pt].to); }    }  }  BOOLCheckdfs (intx) {    if(!reachfromend[x])return false;  for(intPT=FIRST3[X];p t;pt=Lst3[pt].next) {      if(Mustreachend[lst3[pt].to])Continue; if(!checkdfs (lst3[pt].to))return false; }    returnmustreachend[x]=true; }  BOOLCheck () {Predfs (belong[t]); returnCheckdfs (Belong[s]); }};DoubleF[MAXN],G[MAXN];intMAP[MAXN];namespacework{BOOLDONE[MAXN]; Doublef[ the][ the]; intRK; voidSwap (intAintb) {     for(intI=0; i<=rk;++i) swap (f[a][i],f[b][i]); }  voidMultplus (intAintBDoubleTimes ) {     for(intI=0; i<=rk;++i) f[a][i]+=f[b][i]*Times ; }  voidGauss (intN) {RK=N;  for(intI=1; i<=n;++i) {      if(f[i][i]==0){     for(intj=i+1; j<=n;++j) {      if(f[j][i]!=0) {Swap (i,j); Break; }    }      }       for(intj=i+1; j<=n;++j) Multplus (j,i,-f[j][i]/F[i][i]); }     for(inti=n;i>=1;--i) {f[i][0]/=F[i][i];  for(intj=i-1; j>=1;--j) {f[j][0]-=f[j][i]*f[i][0]; }    }  }  voidBuild_equations (intx) {     for(intI=1; i<=sz[x];++i) {       for(intj=0; j<=sz[x];++j) {F[i][j]=0; }    }     for(intI=0; i<sz[x];++i) f[i+1][0]=-G[scc[x][i]];  for(intI=1; i<=sz[x];++i) {F[i][i]=-1; map[scc[x][i-1]]=i; if(scc[x][i-1]==T) f[i][0]=-1; }     for(intI=0; i<sz[x];++i) {      if(scc[x][i]==t)Continue;  for(intPt=first1[scc[x][i]];p t;pt=Lst1[pt].next) {    if(belong[lst1[pt].to]==x) {      if(lst1[pt].to==t)Continue; F[map[lst1[pt].to]][i+1]+=1.0/Outdeg[scc[x][i]]; }      }    }      }  voidDfsintx) {     for(intPT=FIRST2[X];p t;pt=Lst2[pt].next) {      if(!done[lst2[pt].to]) DFS (lst2[pt].to);    } build_equations (x);    Gauss (Sz[x]);  for(intI=0; i<sz[x];++i) {F[scc[x][i]]=f[i+1][0]; }     for(intI=0; i<sz[x];++i) {       for(intPt=first1[scc[x][i]];p t;pt=Lst1[pt].next) {    if(belong[lst1[pt].to]!=x) {G[lst1[pt].to]+=f[scc[x][i]]/Outdeg[scc[x][i]]; }}} Done[x]=true; }}intMain () {scanf ("%d%d%d%d",&n,&m,&s,&t); intb;  for(intI=1; i<=m;++i) {scanf ("%d%d",&a,&b); Addedge1 (a B); Outdeg[a]++;  } Trajan::tarjan (); if(Trajan::check ()) {G[s]=1;    Work::d FS (belong[t]); Doubleans=0;  for(intI=1; i<=n;++i) {//printf ("%.2f", F[i]);ans+=F[i]; }//Ans:the expected number of points on the path from S to Tprintf"%.3f\n", ans-1); }Else{printf ("inf\n"); }  return 0;}

BZOJ2702[SDOI2012] Go Maze

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.