题目
Morenan被困在了一个迷宫里。迷宫可以视为N个点M条边的有向图,其中Morenan处于起点S,迷宫的终点设为T。可惜的是,Morenan非常的脑小,他只会从一个点出发随机沿着一条从该点出发的有向边,到达另一个点。这样,Morenan走的步数可能很长,也可能是无限,更可能到不了终点。若到不了终点,则步数视为无穷大。但你必须想方设法求出Morenan所走步数的期望值。
输入格式
第1行4个整数,N,M,S,T
第[2, M+1]行每行两个整数o1, o2,表示有一条从o1到o2的边。
输出格式
一个浮点数,保留小数点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 7
输出样例
9.500
提示
测试点
N M
[1, 6] <=10 <=100
[7, 12] <=200 <=10000
[13, 20] <=10000 <=1000000
保证强连通分量的大小不超过100
另外,均匀分布着40%的数据,图中没有环,也没有自环
题解
此题和游走那题有异曲同工之妙
我们设\(f[i]\)表示从\(i\)点出发到达终点的期望步数
就有\(f[i] = \frac{\sum (f[to] + 1)}{outde[i]}\)
假若这是一个DAG图,反向dp就可以了
但是如果是一般有向图,我们进行缩点,先计算后面的强联通分量
同一个强联通分量之间,其式子的to要么是之后已经计算好了的点,要么就是强联通分量内部的点,可以用高斯消元解出
判定时,只需要看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;
}