#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <cctype> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #include <bitset> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define pd(a) printf("%d\n", a); #define plld(a) printf("%lld\n", a); #define pc(a) printf("%c\n", a); #define ps(a) printf("%s\n", a); #define MOD 2018 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 1010, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff; int n, m, ss, tt, cnt; int head[maxn], f[maxn][maxn], vis[maxn], pre[maxn], d[maxn], out[maxn]; double p[maxn][maxn]; struct node { int v, next; }Node[maxn << 1]; void add_(int u, int v) { out[u]++; Node[cnt].v = v; Node[cnt].next = head[u]; head[u] = cnt++; } void add(int u, int v) { add_(u, v); add_(v, u); } void spfa(int s) { queue<int> Q; for(int i = 1; i <= n; i++) d[i] = INF; d[s] = 0; mem(vis, 0); Q.push(s); vis[s] = 1; while(!Q.empty()) { int u = Q.front(); Q.pop(); vis[u] = 0; for(int i = head[u]; i != -1; i = Node[i].next) { node e = Node[i]; if(d[e.v] > d[u] + 1 || d[e.v] == d[u] + 1 && u < pre[e.v]) { d[e.v] = d[u] + 1; pre[e.v] = u; if(!vis[e.v]) { vis[e.v] = 1; Q.push(e.v); } } } } for(int i = 1; i <= n; i++) if(i != s) f[i][s] = pre[i]; } double m_dfs(int u, int t) { if(u == t) return p[u][t] = 0; if(f[u][t] == t) return p[u][t] = 1; if(f[f[u][t]][t] == t) return p[u][t] = 1; if(p[u][t] >= -1e-7) return p[u][t]; int nxt = f[f[u][t]][t]; double res = 1; for(int i = head[t]; i != -1; i = Node[i].next) { node e = Node[i]; res += m_dfs(nxt, e.v) /(double) (out[t] + 1); } res += m_dfs(nxt, t) /(double) (out[t] + 1); return p[u][t] = res; } int main() { mem(head, -1); int u, v; cin >> n >> m >> ss >> tt; rap(i, 1, m) { cin >> u >> v; add(u, v); } for(int i = 1; i <= n; i++) spfa(i); mem(p, -1); printf("%.3f\n", m_dfs(ss, tt)); return 0; }
题目总结:
聪聪会走最短路,那么要想到最短路算法,因为每一步都是不确定的, 所以我们可以事先求出所有的每两点的情况,
对期望分布列不明确,没有想到具体的分布列
聪聪和可可 HYSBZ - 1415(概率 + spfa + 记忆化dp)
原文:https://www.cnblogs.com/WTSRUVF/p/9721485.html