Description
Solution
考虑设
fi,j
f
i
,
j
为聪聪在
i
i
节点、可可在节点的期望剩余时间,发现记忆化搜索比较方便:
边界:
- fi,i=0 f i , i = 0
-
fi,j=1(disi,j=1或2)
f
i
,
j
=
1
(
d
i
s
i
,
j
=
1
或
2
)
转移:fi,j=fp,j+∑fp,kdegreej+1 f i , j = f p , j + ∑ f p , k d e g r e e j + 1
其中 p p 为离最近的、标号最小的点, k k 与相连。
Source
/**************************************
* Au: Hany01
* Date: Feb 8th, 2018
* Prob: BZOJ1415 & NOI2005 聪聪和可可
* Email: hany01@foxmail.com
**************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define fir first
#define sec second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define debug(...) fprintf(stderr, __VA_ARGS__)
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
inline void File()
{
#ifdef hany01
freopen("bzoj1415.in", "r", stdin);
freopen("bzoj1415.out", "w", stdout);
#endif
}
const int maxn = 1005, maxm = 2005;
int n, m, s, t, v[maxm], nex[maxm], beg[maxn], deg[maxn], e, las[maxn][maxn], dis[maxn][maxn], vis[maxn];
double dp[maxn][maxn];
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e, ++ deg[uu], ++ deg[vv]; }
inline void SPFA(int s)
{
static deque<int> q;
register int u;
dis[s][s] = 0, Set(vis, 0), q.push_back(s);
while (!q.empty()) {
vis[u = q.front()] = 0, q.pop_front();
for (register int i = beg[u]; i; i = nex[i])
if (chkmin(dis[s][v[i]], dis[s][u] + 1) || (dis[s][v[i]] == dis[s][u] + 1 && u < las[s][v[i]])) {
las[s][v[i]] = u;
if (chkmax(vis[v[i]], 1)) {
if (!q.empty() && dis[s][q.front()] > dis[s][v[i]]) q.push_front(v[i]); else q.push_back(v[i]);
}
}
}
}
inline void Init()
{
register int uu, vv;
n = read(), m = read(), s = read(), t = read(), Set(dis, 127);
while (m --) uu = read(), vv = read(), add(uu, vv), add(vv, uu);
For(i, 1, n) SPFA(i), deg[i] >>= 1;
}
double DFS(int uu, int vv)
{
if (dp[uu][vv] >= -1) return dp[uu][vv];
if (uu == vv) return 0;
if (las[vv][uu] == vv || las[vv][las[vv][uu]] == vv) return 1;
double& Ans = dp[uu][vv]; register int p = las[vv][las[vv][uu]];
Ans = DFS(p, vv);
for (register int i = beg[vv]; i; i = nex[i]) Ans += DFS(p, v[i]);
Ans = Ans / (deg[vv] + 1) + 1;
return Ans;
}
int main()
{
File();
Init();
For(i, 1, n) For(j, 1, n) dp[i][j] = -2;
printf("%.3lf\n", DFS(s, t));
return 0;
}