BZOJ 2707: [SDOI2012]走迷宫 [高斯消元 scc缩点]

2707: [SDOI2012]走迷宫

题意:求s走到t期望步数,\(n \le 10^4\),保证\(|SCC| \le 100\)


求scc缩点,每个scc高斯消元,scc之间直接DP

注意每次清空系数矩阵

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=1e4+5, M=1e6+5;
const double eps=1e-8;
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, de[N], u, v;
struct edge{int v, ne;} e[M];
int cnt=1, h[N];
inline void ins(int u, int v) {e[++cnt]=(edge){v, h[u]}; h[u]=cnt;}
int dfn[N], low[N], dfc, belong[N], scc;
struct List{
	int a[105], n;
	int& operator [](int x) {return a[x];}
	inline void push(int x) {a[++n]=x;}
}li[N];
int st[N], top;
void dfs(int u) { //printf("dfs %d\n",u);
	dfn[u] = low[u] = ++dfc;
	st[++top] = u;
	for(int i=h[u];i;i=e[i].ne) {
		int v=e[i].v;
		if(!dfn[v]) dfs(v), low[u] = min(low[u], low[v]);
		else if(!belong[v]) low[u] = min(low[u], dfn[v]);
	}
	if(dfn[u] == low[u]) {
		scc++;
		while(true) {
			int x=st[top--];
			belong[x] = scc;
			li[scc].push(x);
			if(x == u) break;
		}
	}
}

double a[105][105], f[N]; int id[N];
void gauss(int n) { 
	//puts("\ngauss");
	//for(int i=1; i<=n; i++)
	//	for(int j=1; j<=n+1; j++) printf("%lf%c",a[i][j], j==n+1 ? '\n' : ' ');

	for(int i=1; i<=n; i++) {
		int r=i;
		for(int j=i; j<=n; j++) if(abs(a[j][i])>abs(a[r][i])) r=j;
		if(r!=i) for(int j=1; j<=n+1; j++) swap(a[r][j], a[i][j]);

		for(int k=i+1; k<=n; k++) if(abs(a[k][i]) > eps){
			double t = a[k][i]/a[i][i];
			for(int j=i; j<=n+1; j++) a[k][j] -= t*a[i][j];
		}
	}
	for(int i=n; i>=1; i--) {
		for(int j=n; j>i; j--) a[i][n+1] -= a[i][j]*a[j][n+1];
		a[i][n+1] /= a[i][i];
	}
}
void solve(List &q) {
	memset(a,0,sizeof(a));
	int n=0;
	for(int i=1; i<=q.n; i++) id[q[i]] = ++n;// printf("%d ",q[i]); puts(" q");
	for(int i=1; i<=q.n; i++) {
		int u=q[i]; 
		a[i][i]=1; a[i][n+1]=1;
		if(u==t) {a[i][n+1]=0; continue;}
		for(int p=h[u];p;p=e[p].ne) {
			int v=e[p].v; 
			if(belong[v] != belong[u]) a[i][n+1] += f[v]/de[u];
			else a[i][id[v]] -= 1.0/de[u];
		}
	}
	gauss(n);
	for(int i=1; i<=q.n; i++) f[q[i]] = a[i][n+1];// printf("getf %d %lf\n",q[i],f[q[i]]);
}
namespace SCC {
	struct edge{int v, ne;} e[M];
	int cnt=1, h[N];
	inline void ins(int u, int v) {e[++cnt]=(edge){v, h[u]}; h[u]=cnt;}

	int vis[N];
	void dfs(int u) {
		if(vis[u]) return; vis[u]=1;
		for(int i=h[u];i;i=e[i].ne) dfs(e[i].v);
		//printf("solveSCC %d\n",u);
		solve(li[u]);
	}
}
void build() {
	for(int u=1; u<=n; u++)
		for(int i=h[u];i;i=e[i].ne)
			if(belong[u] != belong[e[i].v]) SCC::ins(belong[u], belong[e[i].v]);
}
int main() {
	freopen("in","r",stdin);
	n=read(); m=read(); s=read(); t=read();
	for(int i=1; i<=m; i++) {
		u=read(), v=read();
		de[u]++; ins(u, v);
	}
	dfs(s);
	//for(int i=1; i<=n; i++) printf("scc %d  %d %d\n",i, dfn[i], belong[i]);
	if(!dfn[t]) {puts("INF"); return 0;}
	for(int i=1; i<=n; i++) if(i!=t && de[i]==0 && dfn[i]) {puts("INF"); return 0;}

	build();
	SCC::dfs(belong[s]);
	//for(int i=1; i<=n; i++) printf("f %d %lf\n",i,f[i]);
	printf("%.3lf", f[s]);
}


posted @ 2017-04-04 22:15  Candy?  阅读(321)  评论(0编辑  收藏  举报