BZOJ 2707: [SDOI2012]走迷宫

设 \(f_i\) 为 \(i \to t\) 的期望步数
转移方程为 \(f_u = \sum_{u \to v} \dfrac{f_v}{outdeg_u} + 1\)
先tarjan缩点
然后强连通分量内进行高斯消元即可
把出边到的点不在强连通分量内的 \(\dfrac{f_v}{outdeg_u}\) 作为常数即可

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define SZ(x) ((int)(x).size())
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) / 2)
#define lowbit(i) ((i) & (-i))
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=((b)-1);i>=(a);i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1000000007, INF = 0x3f3f3f3f;
const ll inf = 0x3f3f3f3f3f3f3f3f;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2, int mod = MOD) {int ans = 1; for (; b; a = 1LL * a * a % mod, b >>= 1)if (b & 1)ans = 1LL * ans * a % mod; return ans % mod;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
	return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
	int x = 0, f = 1; char ch = getc();
	while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
	while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
	return x * f;
}

db mat[111][111];
void gauss(int n) {
	rep (i, 0, n + 1) {
		int r = i;
		rep (j, i + 1, n + 1) {
			if (std::fabs(mat[r][i]) < std::fabs(mat[j][i])) r = j;
		}
		if (r != i) {
			rep (j, 0, n + 2) {
				std::swap(mat[i][j], mat[r][j]);
			}
		}
		rep (j, 0, n + 1) {
			if (j != i) {
				db t = mat[j][i] / mat[i][i];
				rep (k, i, n + 2) mat[j][k] -= mat[i][k] * t;
			}
		}
	}
	rep (i, 0, n + 1) mat[i][n + 1] /= mat[i][i];
}

const int N = 1e4 + 7, E = 1e6 + 7;
db f[N];
Edg
int n, m, s, t;
bool inst[N];
int st[N], top, dfn[N], low[N], dfc, scc, belong[N];
std::vector<int> li[N];
int deg[N];

void dfs(int u) {
	dfn[u] = low[u] = ++dfc;
	st[++top] = u;
	inst[u] = 1;
	es (u, i, v) {
		if (!dfn[v]) {
			dfs(v);
			chkmin(low[u], low[v]);
		} else if (inst[v]) {
			chkmin(low[u], dfn[v]);
		}
	}
	if (low[u] == dfn[u]) {
		++scc;
		int v;
		do {
			v = st[top--];
			inst[v] = 0;
			belong[v] = scc;
			li[scc].pb(v);
		} while (u != v);
	}
}

int id[N];
void done(int cur) {
	memset(mat, 0, sizeof mat);
	int n = 0;
	rep (i, 0, SZ(li[cur])) id[li[cur][i]] = n++;
	rep (i, 0, SZ(li[cur])) {
		int u = li[cur][i];
		mat[i][i] = 1; mat[i][n] = 1;
		if (u == t) { mat[i][n] = 0; continue; }
		es (u, j, v) {
			if (belong[u] != belong[v]) mat[id[u]][n] += f[v] / deg[u];
			else mat[id[u]][id[v]] -= 1.0 / deg[u];
		}
	}
	gauss(n - 1);
	rep (i, 0, SZ(li[cur])) f[li[cur][i]] = mat[i][n];
}
namespace SCC { 
	Edg 
	bool vis[N];
	void solve(int u) {
		if (vis[u]) return;
		vis[u] = 1;
		es (u, i, v)
			solve(v);
		done(u);
	}
}

int main() {
#ifdef LOCAL
	freopen("ans.out", "w", stdout);
#endif
	n = _(), m = _(), s = _(), t = _();
	rep (i, 0, m) {
		int u = _(), v = _();
		addd(u, v);
		deg[u]++;
	}
	dfs(s);
	if (!dfn[t]) return puts("INF"), 0;
	rep (i, 1, n + 1) if (i != t && !deg[i] && dfn[i]) return puts("INF"), 0;
	rep (u, 1, n + 1) {
		es (u, i, v) {
			if (belong[u] != belong[v]) SCC::addd(belong[u], belong[v]);
		}
	}
	SCC::solve(belong[s]);
	printf("%.3f\n", f[s]);
#ifdef LOCAL
	printf("%.10f\n", (db)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}
上一篇:SQLServer中cast函数的使用(参数类型的转换)


下一篇:从0开始学算法--基础算法(3.4分块)