题意:给定一个 n 个结点的有向图,然后从 1 结点出发,从每个结点向每个后继结点的概率是相同的,当走到一个没有后继结点后,那么程序终止,然后问你经过每个结点的期望是次数是多少。

析:假设 i 结点的出度为 di,期望执行次数为 xi,对于一个有 n 个前继结点的 a1, a2, a3 ... an 的结点 i,可以列出方程 xi = xa1/da1 + xa2/da2 + .. + xan/dan,根据每个结点都可以列出一个方程,然后就有 n 个方程,其中结点 1 比较特殊,因为是由它开始的所以看作它有一个前继虚拟结点 0,而 0 只执行一次,所以有 n 个前继结点的 a1, a2, a3 ... an 的结点 1,可以列出方程 x1 = xa1/da1 + xa2/da2 + .. + xan/dan + 1,注意末尾有一个 1,然后就有 n 个方程,然后用高斯消元-约当消元法,注意的是此题可能有无穷多解,和惟一解,多解也就是说最后所以得到的增广矩阵 A[i][i] = 0 && A[i][n] != 0,这样就是无穷大的答案,再就是 A[i][i] = 0 && A[i][n] = 0,这样的话答案就是 0。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #include <numeric>
  21. #define debug() puts("++++")
  22. #define gcd(a, b) __gcd(a, b)
  23. #define lson l,m,rt<<1
  24. #define rson m+1,r,rt<<1|1
  25. #define fi first
  26. #define se second
  27. #define pb push_back
  28. #define sqr(x) ((x)*(x))
  29. #define ms(a,b) memset(a, b, sizeof a)
  30. #define sz size()
  31. #define pu push_up
  32. #define pd push_down
  33. #define cl clear()
  34. #define lowbit(x) -x&x
  35. //#define all 1,n,1
  36. #define FOR(i,n,x) for(int i = (x); i < (n); ++i)
  37. #define freopenr freopen("in.in", "r", stdin)
  38. #define freopenw freopen("out.out", "w", stdout)
  39. using namespace std;
  40.  
  41. typedef long long LL;
  42. typedef unsigned long long ULL;
  43. typedef pair<int, int> P;
  44. const int INF = 0x3f3f3f3f;
  45. const LL LNF = 1e17;
  46. const double inf = 1e20;
  47. const double PI = acos(-1.0);
  48. const double eps = 1e-8;
  49. const int maxn = 100 + 5;
  50. const int maxm = 1e6 + 2;
  51. const LL mod = 1000000007;
  52. const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
  53. const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
  54. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  55. int n, m;
  56. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  57. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  58. inline bool is_in(int r, int c) {
  59. return r >= 0 && r < n && c >= 0 && c < m;
  60. }
  61.  
  62. vector<int> G[maxn];
  63. int d[maxn];
  64. bool ok[maxn];
  65. double A[maxn][maxn];
  66.  
  67. void solve(){
  68. for(int i = 0; i < n; ++i){
  69. int r = i;
  70. for(int j = i+1; j < n; ++j)
  71. if(fabs(A[j][i]) > fabs(A[r][i])) r = j;
  72. if(fabs(A[r][i]) < eps) continue;
  73. if(r != i) for(int j = i; j <= n; ++j) swap(A[r][j], A[i][j]);
  74.  
  75. for(int k = 0; k < n; ++k) if(k != i)
  76. for(int j = n; j >= i; --j) A[k][j] -= A[k][i] / A[i][i] * A[i][j];
  77. }
  78. }
  79.  
  80. int main(){
  81. int kase = 0;
  82. while(scanf("%d", &n) == 1 && n){
  83. for(int i = 0; i < n; ++i) G[i].cl;
  84. ms(d, 0); ms(ok, 0); ms(A, 0); A[0][n] = 1.;
  85. int a, b;
  86. while(scanf("%d %d", &a, &b) == 2 && a+b){
  87. G[b-1].pb(a-1); ++d[a-1];
  88. }
  89. for(int i = 0; i < n; ++i){
  90. A[i][i] = 1.;
  91. for(int j = 0; j < G[i].sz; ++j)
  92. A[i][G[i][j]] -= 1. / d[G[i][j]];
  93. }
  94. solve();
  95. for(int i = n-1; i >= 0; --i){
  96. if(fabs(A[i][i]) < eps && fabs(A[i][n]) > eps){ ok[i] = true; continue; }
  97. for(int j = i+1; j < n; ++j)
  98. if(fabs(A[i][j]) > eps && ok[j]) ok[i] = true;
  99. }
  100. printf("Case #%d:\n", ++kase);
  101. scanf("%d", &m);
  102. int x;
  103. while(m-- && scanf("%d", &x) == 1)
  104. if(ok[x-1]) puts("infinity");
  105. else if(fabs(A[x-1][x-1]) < eps) printf("0.000\n");
  106. else printf("%.3f\n", A[x-1][n] / A[x-1][x-1]);
  107. }
  108. return 0;
  109. }

  

UVa 10828 Back to Kernighan-Ritchie (数学期望 + 高斯消元)的更多相关文章

  1. P4321-随机漫游【状压dp,数学期望,高斯消元】

    正题 题目链接:https://www.luogu.com.cn/problem/P4321 题目大意 给出\(n\)个点\(m\)条边的一张无向图,\(q\)次询问. 每次询问给出一个点集和一个起点 ...

  2. BZOJ 3143 游走 | 数学期望 高斯消元

    啊 我永远喜欢期望题 BZOJ 3143 游走 题意 有一个n个点m条边的无向联通图,每条边按1~m编号,从1号点出发,每次随机选择与当前点相连的一条边,走到这条边的另一个端点,一旦走到n号节点就停下 ...

  3. 【BZOJ】3143: [Hnoi2013]游走 期望+高斯消元

    [题意]给定n个点m条边的无向连通图,每条路径的代价是其编号大小,每个点等概率往周围走,要求给所有边编号,使得从1到n的期望总分最小(求该总分).n<=500. [算法]期望+高斯消元 [题解] ...

  4. 【BZOJ】2337: [HNOI2011]XOR和路径 期望+高斯消元

    [题意]给定n个点m条边的带边权无向连通图(有重边和自环),在每个点随机向周围走一步,求1到n的期望路径异或值.n<=100,wi<=10^9. [算法]期望+高斯消元 [题解]首先异或不 ...

  5. [BZOJ3143][HNOI2013]游走(期望+高斯消元)

    3143: [Hnoi2013]游走 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3576  Solved: 1608[Submit][Status ...

  6. codeforces 24d Broken robot 期望+高斯消元

    题目传送门 题意:在n*m的网格上,有一个机器人从(x,y)出发,每次等概率的向右.向左.向下走一步或者留在原地,在最左边时不能向右走,最右边时不能像左走.问走到最后一行的期望. 思路:显然倒着算期望 ...

  7. 【BZOJ 3143】【Hnoi2013】游走 期望+高斯消元

    如果纯模拟,就会死循环,而随着循环每个点的期望会逼近一个值,高斯消元就通过列方正组求出这个值. #include<cstdio> #include<cctype> #inclu ...

  8. [HNOI2013]游走 期望+高斯消元

    纪念首道期望题(虽说绿豆蛙的归宿才是,但是我打的深搜总觉得不正规). 我们求出每条边的期望经过次数,然后排序,经过多的序号小,经过少的序号大,这样就可以保证最后的值最小. 对于每一条边的期望经过次数, ...

  9. BZOJ2337: [HNOI2011]XOR和路径(期望 高斯消元)

    题意 题目链接 Sol 期望的线性性对xor运算是不成立的,但是我们可以每位分开算 设\(f[i]\)表示从\(i\)到\(n\)边权为1的概率,统计答案的时候乘一下权值 转移方程为 \[f[i] = ...

随机推荐

  1. linux命令学习之:passwd

    passwd命令用于设置用户的认证信息,包括用户密码.密码过期时间等.系统管理者则能用它管理系统用户的密码.只有管理者可以指定用户名称,一般用户只能变更自己的密码. 语法 passwd(选项)(参数) ...

  2. Couchbase学习和使用

    Couchbase介绍 couchbase的关键有两点:延后写入和松散存储.延后写入,顾名思义,couchbase在对数据进行增删时会先体现在内存中,而不会立刻体现在硬盘上,从内存的修改到硬盘的修改这 ...

  3. go语言中container容器数据结构heap、list、ring

    heap堆的使用: package main import ( "container/heap" "fmt" ) type IntHeap []int //我们 ...

  4. abp ef codefirst Value cannot be null. Parameter name: connectionString

    错误原因是abp生成的项目是mvc类型的,但在使用时,选择了vue去开发,所以在abp上重新生成了一个vue项目,把原有的mvc项目给删掉了,没有将新生成的vue类型的项目的文件覆盖掉原有的mvc其他 ...

  5. jQuery的鼠标悬停时放大图片的效果

    这是一个基于jQuery的效果,当鼠标在小图片上悬停时,会弹出一个大图,该大图会跟随鼠标的移动而移动.这个效果最初源于小敏同志的一个想法,刚开始做的时候只能实现弹出的图片是固定的,不能随鼠标移动,最后 ...

  6. Get、Post 提交的乱码问题

    1.问题 在spring mvc开发的时候出现乱码问题: 2.解决方案 (1)Get提交:tomcat容器接收的造成的乱码问题,修改server.xml文件: (2)Post提交:在web.xml中配 ...

  7. 设置漂亮的eclipse主题(Theme)风格

    看看这些主题: Eclipse Color Themes 设置步骤: 1.点击help --> Eclipse Marketplace... 2.搜索Color Eclipse Themes 3 ...

  8. FoxMail提示:请求的名称有效,但是找不到请求的类型的数据

    FoxMail发送或者接收邮件的时候,提示如下信息: <错误信息:请求的名称有效,但是找不到请求的类型的数据> 一,DNS解析不稳定 解决办法:修改本地电脑上面本地连接中的DNS地址< ...

  9. PS合成的5个要点:场景、对比、氛围、模糊、纹理

    是否觉得做合成打开PS之后无处下手,做完之后总觉得缺少故事情节?这一次分享的5个要点,是个人觉得需要重视的,每一点都有一个案例来让作品变得多一份惊喜.(申明:文中素材均来自网络,这里仅作分享交流作用) ...

  10. tmp下莫名其妙生成root权限的缓存文件

    现象: 原因:跟ngnix.conf有关,跟tmp的owner有关