╰( ̄▽ ̄)╭
Morenan was trapped in a maze.
The maze can be regarded as a directed graph with N points M edges, where Morena n is at the starting point S and the end of the maze is set to T.
Unfortunately, Morenan is so small that he can only start from one point and randomly follow a directed edge from that point to another.
In this way, Morenan may walk a long, infinite number of steps, more likely not to reach the end.
If the end point is not reached, the number of steps is regarded as infinite.
But you have to figure out the expected number of steps Morenan takes.
(⊙ ▽ ⊙)
Looking at the question at the beginning, I think it's a problem on DAG after tarjan shrinkage point.
When the original graph is DAG
Let fi denote the distance from point i to the end.
It is easy to have fi=j [next(i)1|next(i) | (fj+1),
next(i) is the successor set of I.
It is easy to use topological sorting to complete dynamic programming.
When the original graph is a general digraph
The strong connected components in the graph can be found by tarjan algorithm.
For any strongly connected component, we use Gauss elimination to solve the f value of each point in the strongly connected component.
Setting up the topological sort can solve this problem.
The time complexity is O(n L3), where L is the largest strongly connected component.
The actual time complexity is far less.
( ̄~ ̄)
Gauss elimination:
1. target
Solving the n-element first-order equation.
2. steps
First:
The coefficients of the first element of an equation are reduced to one, which can eliminate the first element of other equations.
By analogy, the second yuan and the third yuan can be eliminated...
Finally, there will be only one equation about the last element, which is actually the root of this element.
Then:
When the root of the last element is obtained, it can be substituted into the previous equation.
So we can calculate the roots of all the elements.
(⊙v⊙)
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<math.h>
#define ll long long
using namespace std;
const char* fin="jzoj2758.in";
const char* fout="jzoj2758.out";
const int Inf=0x7fffffff;
const int maxn=20007,maxm=2000007,maxk=107;
int n,m,St,En,i,j,k,N,ti;
int fi[maxn],ne[maxm],la[maxm],tot;
int Fi[maxn],Ne[maxm],La[maxm],Tot;
int dfn[maxn],low[maxn],num,st[maxn],be[maxn];
bool bz[maxn],End;
int inf[maxn];
int az[maxn];
int ha[maxn][maxk],ru[maxn],id[maxn],tow[maxn];
double f[maxn],gs[maxk][maxk];
int b[maxn],head,tail;
void add_line(int a,int b){
tot++;
ne[tot]=fi[a];
la[tot]=b;
fi[a]=tot;
}
void Add_line(int a,int b){
Tot++;
Ne[Tot]=Fi[a];
La[Tot]=b;
Fi[a]=Tot;
}
void tarjan(int v){
int i,j,k;
dfn[v]=low[v]=++num;
bz[st[j=++st[0]]=v]=true;
for (k=fi[v];k;k=ne[k])
if (!dfn[la[k]]){
tarjan(la[k]);
low[v]=min(low[v],low[la[k]]);
}else if (bz[la[k]]) low[v]=min(low[la[k]],low[v]);
if (low[v]==dfn[v]){
N++;
be[N]=0;
while (st[0]>=j){
ha[N][++ha[N][0]]=st[st[0]];
be[st[st[0]]]=N;
bz[st[st[0]--]]=false;
}
}
}
double ABS(double x){
return x>0?x:-x;
}
void add(int v){
b[++tail]=v;
}
void count(int v,int n){
int i,j,k,l;
double tmp,tmd;
ti++;
for (i=1;i<=n-1;i++){
for (j=1;j<n;j++){
if (az[j]<ti && (gs[j][i]>10e-13 || gs[j][i]<-10e-13)){
az[j]=ti;
tow[i]=j;
tmp=1.0/gs[j][i];
for (k=1;k<=n;k++) gs[j][k]*=tmp;
for (k=1;k<n;k++){
if (k==j) continue;
tmd=gs[k][i];
for (l=1;l<=n;l++)
gs[k][l]-=tmd*gs[j][l];
}
break;
}
}
}
for (i=n-1;i>=1;i--){
f[ha[v][tow[i]]]=-gs[tow[i]][n];
for (j=1;j<=n-1;j++){
gs[j][n]-=gs[j][i]*gs[tow[i]][n];
}
}
}
void work(int v){
int i,j,k;
for (i=1;i<=ha[v][0];i++) id[ha[v][i]]=i;
for (i=1;i<=ha[v][0];i++){
for (j=1;j<=ha[v][0]+1;j++) gs[i][j]=0;
j=0;
for (k=Fi[ha[v][i]];k;k=Ne[k]){
gs[i][ha[v][0]+1]+=1;
gs[i][i]-=1;
if (f[La[k]]>10e-7 || La[k]==En) gs[i][ha[v][0]+1]+=f[La[k]];
else gs[i][id[La[k]]]+=1;
}
}
count(v,ha[v][0]+1);
}
void update(int v){
int i,j,k;
for (i=1;i<=ha[v][0];i++)
for (k=fi[ha[v][i]];k;k=ne[k]){
inf[be[la[k]]]=max(inf[be[la[k]]],inf[v]);
if (!--ru[be[la[k]]]) add(be[la[k]]);
}
}
void topsort(){
int i,j,k;
head=tail=0;
memset(f,0,sizeof(f));
f[En]=0;
for (i=n+1;i<=N;i++)
if (!ru[i]){
add(i);
}
while (head++<tail){
if (be[En]==b[head]) inf[b[head]]=1;
if (inf[b[head]]==0) inf[b[head]]=2;
if (inf[b[head]]==1 && b[head]!=be[En]) work(b[head]);
update(b[head]);
}
}
int main(){
scanf("%d%d%d%d",&n,&m,&St,&En);
N=n;
for (i=1;i<=m;i++){
scanf("%d%d",&j,&k);
if (j!=En){
Add_line(j,k);
add_line(k,j);
}
}
for (i=1;i<=n;i++) if (!dfn[i]) tarjan(i);
for (i=1;i<=n;i++)
for (k=fi[i];k;k=ne[k])
if (be[la[k]]!=be[i]) ru[be[la[k]]]++;
topsort();
//printf("%3.lf",f[St]);
if (inf[be[St]]!=1) printf("INF");
else printf("%.3lf",f[St]);
return 0;
}