设为首页 加入收藏

TOP

BZOJ2707: [SDOI2012]走迷宫(期望 tarjan 高斯消元)
2019-01-14 18:08:36 】 浏览:77
Tags: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;
}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇题解 洛谷P3622/BZOJ1151【[APIO2.. 下一篇cf113D. Museum(期望 高斯消元)

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目