bzoj2707 走迷宫
嘟嘟嘟
这题挺好想的,就是特别难写。
首先如果\(n \leqslant 100\),就是一个人人都会的\(O(n ^ 3)\)高斯消元。但现在\(n \leqslant 10000\)就不行了,不过数据给了提示,告诉我们强连通分量的大小最大为100。这启发我们首先得tarjan缩点,建出DAG。
然后我们观察自己列的方程,发现对于每一个强连通分量,未知数只有这个scc里的点和连出去的边到达的点。那么对于出度为0的scc,可以直接高斯消元求解。
这样我们建出反图,按照拓扑序对于每一个scc高斯消元,此题就做完了。复杂度\(O(100 ^ 3 * 100)\),非常担心bzoj能不能过,但还好他是过了的。
这里得考虑无解的情况。一是起点终点不连通;二是存在一个点,起点能到达,但是到不了终点,这样如果走入了这个点所在的scc就肯定走不到终点了。
刚开始没想清楚无解的第二种情况,就一直WA。前前后后写了两个点。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const db INF = 1e14;
const db eps = 1e-10;
const int maxn = 1e4 + 5;
const int maxs = 105;
const int maxe = 2e6 + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen("ha.in", "r", stdin);
freopen("ha.out", "w", stdout);
#endif
}
int n, m, s, t, du[maxn];
struct Edge
{
int nxt, to;
}e[maxe];
int head[maxn], ecnt = -1;
In void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
}
bool vis[maxn];
In void dfs_con(int now)
{
vis[now] = 1;
for(int i = head[now], v; ~i; i = e[i].nxt)
if(!vis[v = e[i].to]) dfs_con(v);
}
bool in[maxn];
int st[maxn], top = 0;
int dfn[maxn], low[maxn], cnt = 0;
int col[maxn], ccol = 0;
vector<int> scc[maxn];
In void dfs(int now)
{
st[++top] = now; in[now] = 1;
dfn[now] = low[now] = ++cnt;
for(int i = head[now], v; ~i; i = e[i].nxt)
{
if(!dfn[v = e[i].to])
{
dfs(v);
low[now] = min(low[now], low[v]);
}
else if(in[v]) low[now] = min(low[now], dfn[v]);
}
if(dfn[now] == low[now])
{
int x; ++ccol;
do
{
x = st[top--]; in[x] = 0;
col[x] = ccol;
scc[ccol].push_back(x);
}while(x ^ now);
}
}
Edge e2[maxe];
int head2[maxn], ecnt2 = -1, du2[maxn];
In void addEdge2(int x, int y)
{
e2[++ecnt2] = (Edge){head2[x], y};
head2[x] = ecnt2;
}
In void buildGraph(int now) //建反图
{
int u = col[now];
for(int i = head[now], v; ~i; i = e[i].nxt)
if(u ^ (v = col[e[i].to])) addEdge2(v, u), ++du2[u];
}
db Ans[maxn];
int id[maxn], pos[maxn], cnt2;
db f[maxs][maxs];
int _Max = 0;
In void build2(int u)
{
cnt2 = 0;
for(int i = 0; i < (int)scc[u].size(); ++i) pos[++cnt2] = scc[u][i], id[scc[u][i]] = cnt2;
Mem(f, 0);
for(int i = 0; i < (int)scc[u].size(); ++i)
{
int now = scc[u][i];
f[id[now]][id[now]] = f[id[now]][cnt2 + 1] = 1;
for(int j = head[now], v; ~j; j = e[j].nxt)
if(col[v = e[j].to] ^ u) f[id[now]][cnt2 + 1] += 1.0 / du[now] * Ans[v];
else f[id[now]][id[v]] -= 1.0 / du[now];
}
if(col[t] == u) Mem(f[id[t]], 0), f[id[t]][id[t]] = 1;
}
In void Gauss(int u)
{
build2(u);
for(int i = 1; i <= cnt2; ++i)
{
int pos = i;
for(int j = i + 1; j <= cnt2; ++j)
if(fabs(f[j][i]) > fabs(f[pos][i])) pos = j;
if(pos ^ i) swap(f[pos], f[i]);
if(fabs(f[i][i]) < eps) continue;
db tp = f[i][i];
assert(fabs(tp) > eps);
for(int j = i; j <= cnt2 + 1; ++j) f[i][j] /= tp;
for(int j = i + 1; j <= cnt2; ++j)
{
db tp = f[j][i];
for(int k = i; k <= cnt2 + 1; ++k) f[j][k] -= tp * f[i][k];
}
}
for(int i = cnt2; i > 0; --i)
{
Ans[pos[i]] = f[i][cnt2 + 1];
for(int j = i - 1; j > 0; --j) f[j][cnt2 + 1] -= f[j][i] * f[i][cnt2 + 1];
}
}
In void topo()
{
queue<int> q; q.push(col[t]);
while(!q.empty())
{
int now = q.front(); q.pop();
Gauss(now);
for(int i = head2[now], v; ~i; i = e2[i].nxt)
if(!--du2[v = e2[i].to]) q.push(v);
}
}
int main()
{
//MYFILE();
Mem(head, -1), Mem(head2, -1);
n = read(), m = read(), s = read(), t = read();
for(int i = 1; i <= m; ++i)
{
int x = read(), y = read();
addEdge(x, y); ++du[x];
}
for(int i = 1; i <= n; ++i) if(!dfn[i]) dfs(i);
dfs_con(1);
if(!vis[t]) {puts("INF"); return 0;}
for(int i = 1; i <= n; ++i) buildGraph(i);
for(int i = 1; i <= n; ++i)
if(vis[i] && !du2[col[i]] && col[i] != col[t]) {puts("INF"); return 0;}
topo();
if(Ans[s] == INF) puts("INF");
else printf("%.3lf\n", Ans[s]);
return 0;
}