Dudu's maze
Posted czy-power
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dudu's maze相关的知识,希望对你有一定的参考价值。
To seek candies for Maomao, Dudu comes to a maze. There are nn rooms numbered from 11 to nn and mm undirected roads.
There are two kinds of rooms in the maze -- candy room and monster room. There is one candy in each candy room, and candy room is safe. Dudu can take the only candy away when entering the room. After he took the candy, this candy room will be empty. A empty room is also safe. If Dudu is in safe, he can choose any one of adjacent rooms to go, whatever it is. Two rooms are adjacent means that at least one road connects the two rooms.
In another kind of rooms, there are fierce monsters. Dudu can‘t beat these monsters, but he has a magic portal. The portal can show him a randomly chosen road which connects the current room and the other room.
The chosen road is in the map so Dudu know where it leads to. Dudu can leave along the way to the other room, and those monsters will not follow him. He can only use the portal once because the magic energy is not enough.
Dudu can leave the maze whenever he wants. That‘s to say, if he enters a monster room but he doesn‘t have enough energy to use the magic portal, he will choose to leave the maze immediately so that he can save the candies he have. If he leave the maze, the maze will never let him in again. If he try to fight with the monsters, he will be thrown out of the maze (never let in, of course). He remembers the map of the maze, and he is a clever guy who can move wisely to maximum the expection of candies he collected.
Maomao wants to know the expected value of candies Dudu will bring back. Please tell her the answer. He will start his adventure in room 1, and the room 1 is always a candy room. Since there may be more than one road connect the current room and the room he wants to go to, he can choose any of the roads.
Input
First line a integer tt, means tt cases. 1 \le t \le 51≤t≤5
For each case:
First line 3 integer nn, mm, kk, nn means the number of rooms, mm means the number of roads, kk means the number of monster rooms. 1 \le n \le 1000001≤n≤100000, n-1 \le m \le 2*nn−1≤m≤2∗n, 0 \le k \le n0≤k≤n
Next mm lines, for each line there are two integer aa and bb, separated by a space, means there is a road between aa and bb. There may be repeated edges, but won‘t be self loop. 1 \le a,b \le n1≤a,b≤n
In the last line there are kk distinct numbers, the ii-th number x_ixi? means the number of the ii-th monster room is x_ixi?, and room 1 won‘t be monster room. 1 \le i \le k1≤i≤k
Output
For each case output a real number. The absolute error of the answer should not exceed 10^-610−6.
样例输入
2 7 6 2 1 2 1 3 2 5 2 4 3 6 4 7 2 3 7 7 2 1 2 1 3 2 5 2 4 3 6 4 7 2 4 2 3
样例输出
2.000000 2.250000
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int mod = 1000000007; const int maxn = 3e5; int T, n, m, k; struct node int to, nx; o[maxn]; int tot, head[maxn], is[maxn], vis[maxn], cnt, re[maxn], fa[maxn], e[maxn], use[maxn]; int deg[maxn]; double res = 1.0; inline void init() tot = 0; cnt = 0; res = 1.0; memset(head, 0, sizeof(head)); memset(vis, 0, sizeof(vis)); for (register int i = 1; i <= n; ++i) fa[i] = i, e[i] = 1, deg[i] = 0; is[i] = 0; use[i] = 0; inline void add_edge(int u, int v) o[++tot].to = v; o[tot].nx = head[u]; head[u] = tot; inline void bfs() queue<int> q; q.push(1); vis[1] = 1; while (!q.empty()) int cur = q.front(); q.pop(); for (register int i = head[cur]; i; i = o[i].nx) int to = o[i].to; if (vis[to])continue; if (is[to]) re[++cnt] = to; vis[to] = 1; continue; q.push(to); vis[to] = 1; ++res; inline int find_fa(int x) return x == fa[x] ? x : fa[x] = find_fa(fa[x]); inline void dfs(int cur) if (use[cur])return; use[cur] = 1; for (register int i = head[cur]; i; i = o[i].nx) int to = o[i].to; if (vis[to] || is[to] || use[to])continue; //vis[to] = 1; dfs(to); use[to] = 1; int f_cur = find_fa(cur); int f_to = find_fa(to); if (f_cur != f_to) fa[f_to] = f_cur; e[f_cur] += e[f_to]; int main() #ifndef INLINE_JUDGE //freopen("1.txt", "r", stdin); #endif scanf("%d", &T); while (T--) scanf("%d%d%d", &n, &m, &k); init(); for (register int i = 1, u, v; i <= m; ++i) scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); ++deg[u]; ++deg[v]; for (register int i = 1, x; i <= k; ++i) scanf("%d", &x); is[x] = 1; bfs(); double mx = 0; for (register int i = 1; i <= cnt; ++i) int cur = re[i]; double pre = 0; for (register int j = head[cur]; j; j = o[j].nx) int to = o[j].to; if (vis[to] || is[to])continue; dfs(to); pre += e[find_fa(to)]; mx = max(mx, pre / (double) deg[cur]); printf("%f\n", res + mx); return 0;
以上是关于Dudu's maze的主要内容,如果未能解决你的问题,请参考以下文章