博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【HNOI2013】游走
阅读量:4539 次
发布时间:2019-06-08

本文共 2071 字,大约阅读时间需要 6 分钟。

题解

图上的期望大部分是\(dp\),无向图的期望大部分是高斯消元

\(f[i]\)表示走到点\(i\)的期望,\(d[i]\)表示\(i\)的度,\(to(i)\)表示\(i\)能到达的点集

所以\(f[i] = \sum\limits_{x \in to(i)} f[x] / d[x]\)

然后每个点能够列出这样的方程,直接高斯消元就可以了

代码

#include
#define RG register#define clear(x, y) memset(x, y, sizeof(x));using namespace std;inline int read(){ int data = 0, w = 1; char ch = getchar(); while(ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if(ch == '-') w = -1, ch = getchar(); while(ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar(); return data*w;}const int maxn(510), maxm(250100);struct edge { int next, to; } e[maxm << 1];int head[maxn], e_num;inline void add_edge(int from, int to) { e[++e_num] = {head[from], to}; head[from] = e_num; }double a[maxn][maxn], ans[maxm], Ans, deg[maxn];int n, m, from[maxm], to[maxm];inline void Gauss(){ for(RG int i = 1, k = i; i <= n; i++, k = i) { for(RG int j = k + 1; j <= n; j++) if(fabs(a[k][i]) < fabs(a[j][i])) k = j; swap(a[i], a[k]); for(RG int j = i + 1; j <= n + 1; j++) a[i][j] /= a[i][i]; a[i][i] = 1.; for(RG int j = 1; j <= n; j++) { if(i == j) continue; for(RG int k = i + 1; k <= n + 1; k++) a[j][k] -= a[j][i] * a[i][k]; a[j][i] = 0.; } }}int main(){ n = read(); m = read(); for(RG int i = 1; i <= m; i++) { from[i] = read(); to[i] = read(); add_edge(from[i], to[i]); deg[from[i]] += 1.; add_edge(to[i], from[i]); deg[to[i]] += 1.; } for(RG int i = 1; i < n; i++) { for(RG int j = head[i]; j; j = e[j].next) if(e[j].to != n) a[i][e[j].to] += -1. / deg[e[j].to]; a[i][i] = 1; } a[n][n] = 1; a[1][n + 1] = 1; Gauss(); for(RG int i = 1; i <= m; i++) ans[i] = ((from[i] == n) ? 0 : a[from[i]][n + 1] / deg[from[i]]) + ((to[i] == n) ? 0 : a[to[i]][n + 1] / deg[to[i]]); sort(ans + 1, ans + m + 1); for(RG int i = 1; i <= m; i++) Ans += (m - i + 1) * ans[i]; printf("%.3lf\n", Ans); return 0;}

转载于:https://www.cnblogs.com/cj-xxz/p/10396422.html

你可能感兴趣的文章
我是怎么定义微服务平台?
查看>>
python random
查看>>
互联网技术
查看>>
input输入框只允许输入数字/ 数字+小数点/ 文字+字母/ 等解决方法
查看>>
【翻译】西川善司「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,前篇(2)...
查看>>
函数名、闭包及迭代器
查看>>
mysql 5.6 参数详解
查看>>
求旋转数组的最小元素
查看>>
jQuery ajax error函数(交互错误信息的获取)
查看>>
Gson解析Json数组
查看>>
Lintcode: Fast Power
查看>>
Pocket Gem OA: Log Parser
查看>>
枚举也能直接转换为对应的数值输出
查看>>
angularjs1-7,供应商
查看>>
BitSet
查看>>
Spring常用注解,自动扫描装配Bean
查看>>
(转载)深入理解WeakHashmap
查看>>
JAVA中的数组
查看>>
爬虫—使用Requests
查看>>
scrollIntoView()窗口滚动
查看>>