1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include <bits/stdc++.h> #define MAX_N 1000 #define INF 0x3f3f3f3f using namespace std; typedef double dnt; template <class T> inline void read(T &x) { x = 0; int c = getchar(), f = 1; for (; !isdigit(c); c = getchar()) if (c == 45) f = -1; for (; isdigit(c); c = getchar()) (x *= 10) += f*(c-'0'); } int n, m, sc, sk; vector <int> G[MAX_N+5]; int dis[MAX_N+5][MAX_N+5]; int nxt[MAX_N+5][MAX_N+5]; dnt d[MAX_N+5], f[MAX_N+5][MAX_N+5]; void BFS(int s) { queue <int> que; que.push(s), dis[s][s] = 0; while (!que.empty()) { int u = que.front(); que.pop(); for (int i = 0, v; i < (int)G[u].size(); i++) if (dis[s][v = G[u][i]] == INF) dis[s][v] = dis[s][u]+1, que.push(v); } } dnt DP(int c, int k) { dnt &ret = f[c][k]; if (ret >= 0) return ret; if (c == k) return ret = 0; if (dis[c][k] <= 2) return ret = 1; ret = 0; for (int i = 0, t; i < (int)G[k].size(); i++) t = G[k][i], ret += DP(nxt[nxt[c][k]][k], t)/(d[k]+1); return ret += DP(nxt[nxt[c][k]][k], k)/(d[k]+1)+1; } int main() { read(n), read(m), read(sc), read(sk); for (int i = 1, u, v; i <= m; i++) read(u), read(v), G[u].push_back(v), d[u]++, G[v].push_back(u), d[v]++; memset(dis, INF, sizeof dis); for (int i = 1; i <= n; i++) BFS(i); for (int c = 1; c <= n; c++) for (int k = 1; k <= n; k++) for (int i = 0, t; i < (int)G[c].size(); i++) if (dis[t = G[c][i]][k] < dis[nxt[c][k]][k] || !nxt[c][k]) nxt[c][k] = t; else if (dis[t][k] == dis[nxt[c][k]][k] && t < nxt[c][k]) nxt[c][k] = t; memset(f, -1, sizeof f); return printf("%.3lf\n", DP(sc, sk)), 0; }
|