BZOJ2707: [SDOI2012]走迷宫(期望 tarjan 高斯消元)
题意
Sol
设\(f[i]\)表示从\(i\)走到\(T\)的期望步数
显然有\(f[x] = \sum_{y} \frac{f[y]}{deg[x]} + 1\)
证明可以用全期望公式。
那么我们可以把每个强联通分量里的点一起高斯消元,就做完了。
(warning:BZOJ没有C++11,但是下面的代码是正确的,至于为什么可以点题目链接。。。。)
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, S, T;
int dfn[MAXN], low[MAXN], vis[MAXN], tot, cnt, inder[MAXN], col[MAXN], ha[MAXN];
double f[201][201], deg[MAXN], ans[MAXN];
stack<int> s;
vector<int> v[MAXN], scc[MAXN], E[MAXN];
void Pre() {
for(int i = 1; i <= N; i++) {
double sum = 0; f[i][i] = 1.0;
for(auto &x : v[i])
if(x != T) sum += 1.0 / deg[x], f[i][x] = -1.0;
f[i][N + 1] = sum;
}
}
void Gauss(int n) {
for(int i = 1; i <= n; i++) {
int mx = i;
for(int j = i + 1; j <= n; j++) if(f[j][i] > f[mx][i]) mx = j;
if(i != mx) swap(f[i], f[mx]);
for(int j = 1; j <= n; j++) {
if(i == j) continue;
double p = f[j][i] / f[i][i];
for(int k = i + 1; k <= n + 1; k++) f[j][k] -= f[i][k] * p;
}
}
for(int i = 1; i <= n; i++) f[i][n + 1] = f[i][n + 1] / f[i][i];
}
void Tarjan(int x) {
dfn[x] = low[x] = ++tot; s.push(x); vis[x] = 1;
for(auto &to : v[x]) {
if(!dfn[to]) Tarjan(to), low[x] = min(low[x], low[to]);
else if(vis[to]) low[x] = min(low[x], dfn[to]);
}
if(low[x] == dfn[x]) {
int h; cnt++;
do {
h = s.top(); s.pop();
vis[h] = 0;
scc[cnt].push_back(h);
col[h] = cnt;
}while(h != x);
}
}
void solve(vector<int> &p) {
memset(vis, 0, sizeof(vis));
memset(f, 0, sizeof(f)); int num = p.size();
for(int i = 0; i < p.size(); i++) vis[p[i]] = i + 1;
for(int i = 0; i < p.size(); i++) {
int x = p[i];
f[i + 1][i + 1] = deg[x]; f[i + 1][num + 1] = deg[x];
for(auto &to : v[x]) {
if(vis[to]) f[i + 1][vis[to]] -= 1;
else f[i + 1][num + 1] += ans[to];
}
}
Gauss(num);
for(int i = 0; i < p.size(); i++) ans[p[i]] = f[i + 1][num + 1];
}
void Topsort() {
queue<int> q; q.push(col[T]);
while(!q.empty()) {
int p = q.front(); q.pop();
for(auto &to : E[p]) if(!(--inder[to])) q.push(to);
if(p != col[T])
solve(scc[p]);
}
}
int main() {
N = read(); M = read(); S = read(); T = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read();
if(x != T) v[x].push_back(y), deg[x]++;
}
Tarjan(S);
if(!dfn[T]) {puts("INF"); return 0;}
for(int i = 1; i <= N; i++) {
for(auto &x : v[i])
if(col[i] != col[x])
inder[col[i]]++, E[col[x]].push_back(col[i]);
}
for(int i = 1; i <= cnt; i++) if(i != col[T] && !inder[i]) {puts("INF"); return 0;}
Topsort();
printf("%.3lf", ans[S]);
return 0;
}
作者:自为风月马前卒
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。