Morenan被困在了一個迷宮裏。迷宮能夠視爲N個點M條邊的有向圖,其中Morenan處於起點S,迷宮的終點設爲T。惋惜的是,Morenan很是的腦小,他只會從一個點出發隨機沿着一條從該點出發的有向邊,到達另外一個點。這樣,Morenan走的步數可能很長,也多是無限,更可能到不了終點。若到不了終點,則步數視爲無窮大。但你必須千方百計求出Morenan所走步數的指望值。ios
第1行4個整數,N,M,S,T
第[2, M+1]行每行兩個整數o1, o2,表示有一條從o1到o2的邊。c++
一個浮點數,保留小數點3位,爲步數的指望值。若指望值爲無窮大,則輸出"INF"。測試
9 12 1 9
1 2
2 3
3 1
3 4
3 7
4 5
5 6
6 4
6 7
7 8
8 9
9 7ui
9.500spa
測試點
N M
[1, 6] <=10 <=100code
[7, 12] <=200 <=10000ci
[13, 20] <=10000 <=1000000
保證強連通份量的大小不超過100
另外,均勻分佈着40%的數據,圖中沒有環,也沒有自環get
此題和遊走那題有殊途同歸之妙
咱們設\(f[i]\)表示從\(i\)點出發到達終點的指望步數
就有\(f[i] = \frac{\sum (f[to] + 1)}{outde[i]}\)string
倘若這是一個DAG圖,反向dp就能夠了
可是若是是通常有向圖,咱們進行縮點,先計算後面的強聯通份量
同一個強聯通份量之間,其式子的to要麼是以後已經計算好了的點,要麼就是強聯通份量內部的點,能夠用高斯消元解出it
斷定時,只須要看S出發到達的全部點可否都到達T就能夠了
寫起來真要命
T出發的邊沒有任何意義,不要添加,不然可能會致使方程組無解
我醜陋雜亂的代碼
#include<iostream> #include<cstdio> #include<cmath> #include<vector> #include<cstring> #include<algorithm> #define eps 1e-8 #define LL long long int #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt) #define REP(i,n) for (int i = 1; i <= (n); i++) #define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts(""); using namespace std; const int maxn = 10005,maxm = 1000005,INF = 1000000000; inline int read(){ int out = 0,flag = 1; char c = getchar(); while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();} while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();} return out * flag; } int h[maxn],ne = 2; int h2[maxn],ne2 = 2; int h3[maxn],ne3 = 2; int n,m,S,T; int inde[maxn]; double de[maxn]; struct EDGE{int to,nxt;}ed[maxm],ed2[maxm],ed3[maxm]; inline void build(int u,int v){ if (u == T) return; ed[ne] = (EDGE){v,h[u]}; h[u] = ne++; de[u] += 1; ed3[ne3] = (EDGE){u,h3[v]}; h3[v] = ne3++; } inline void add(int u,int v){ ed2[ne2] = (EDGE){v,h2[u]}; h2[u] = ne++; } int dfn[maxn],low[maxn],st[maxn],Scc[maxn],scci,top,cnt; vector<int> scc[maxn]; void dfs(int u){ dfn[u] = low[u] = ++cnt; st[++top] = u; Redge(u){ if (!dfn[to = ed[k].to]){ dfs(to); low[u] = min(low[u],low[to]); }else if (!Scc[to]) low[u] = min(low[u],dfn[to]); } if (low[u] == dfn[u]){ scci++; do{ Scc[st[top]] = scci; scc[scci].push_back(st[top]); }while (st[top--] != u); } } void tarjan(){ for (int i = 1; i <= n; i++) if (!dfn[i]) dfs(i); } void rebuild(){ for (int i = 1; i <= n; i++){ int u = Scc[i]; Redge(u) if (Scc[to = ed[k].to] != u) add(Scc[to],u); } } int vis[maxn]; void dfs1(int u){ vis[u] = 1; Redge(u) if (!vis[to = ed[k].to]) dfs1(to); } void dfs2(int u){ vis[u] += 2; for (int k = h3[u],to; k; k = ed3[k].nxt) if (vis[to = ed3[k].to] <= 1) dfs2(to); } bool check(){ dfs1(S); dfs2(T); for (int i = 1; i <= n; i++) if (vis[i] == 1) return false; return true; } double f[maxn]; double A[205][205]; int id[maxn],c[maxn]; void gause(int u){ int cnt = scc[u].size(); for (int i = 1; i <= cnt; i++){ c[i] = scc[u][i - 1]; id[c[i]] = i; } for (int i = 1; i <= cnt + 1; i++) for (int j = 1; j <= cnt + 1; j++) if (i != j) A[i][j] = 0; else A[i][j] = 1; for (int i = 1; i <= cnt; i++){ Redge(c[i]){ to = ed[k].to; if (Scc[to] != u) A[i][cnt + 1] += (f[to] + 1) / de[c[i]]; else A[i][id[to]] -= 1 / de[c[i]],A[i][cnt + 1] += 1 / de[c[i]]; } } for (int i = 1; i <= cnt; i++){ int j = i; while (j <= cnt && fabs(A[j][i]) <= eps) j++; if (j == cnt + 1){ puts("INF"); exit(0); } if (j != i) for (int k = 1; k <= cnt + 1; k++) swap(A[i][k],A[j][k]); for (int j = i + 1; j <= cnt; j++){ if (fabs(A[j][i]) > eps){ double t = A[j][i]; for (int k = i; k <= cnt + 1; k++) A[j][k] = A[j][k] / t * A[i][i]; for (int k = i; k <= cnt + 1; k++) A[j][k] -= A[i][k]; } } } for (int i = cnt; i; i--){ for (int j = i + 1; j <= cnt; j++) A[i][cnt + 1] -= A[i][j] * f[c[j]]; if (fabs(A[i][i]) <= eps){ puts("INF"); exit(0); } A[i][cnt + 1] /= A[i][i]; f[c[i]] = A[i][cnt + 1]; } } void solve(){ for (int i = 1; i <= scci; i++) gause(i); printf("%.3lf\n",f[S]); } int main(){ n = read(); m = read(); S = read(); T = read(); int a,b; while (m--){ a = read(); b = read(); build(a,b); } if (!check()){ puts("INF"); return 0; } tarjan(); rebuild(); solve(); return 0; }