Back to Kernighan-Ritchie
Input: Standard Input

Output: Standard Output

You must have heard the name of Kernighan and Ritchie, the authors of The C Programming Language. While coding in C, we use different control statements and loops, such as, if-then-elsefordo-while, etc. Consider the following fragment of pseudo code:

//execution starts here

do {

U;

V;

} while(condition);

W;

In the above code, there is a bias in each conditional branch. Such codes can be represented by control flow graphs like below:

Let the probability of jumping from one node of the graph to any of its adjacent nodes be equal. So, in the above code fragment, the expected number of times U executes is 2. In this problem, you will be given with such a control flow graph and find the expected number of times a node is visited starting from a specific node.

Input

Input consists of several test cases. There will be maximum 100 test cases. Each case starts with an integer: n (n ≤ 100). Here n is the number of nodes in the graph. Each node in the graph is labeled with 1 ton and execution always starts from 1. Each of the next few lines has two integers: start and end which means execution may jump from node startto node end. A value of zero for start ends this list. After this, there will be an integer q (q ≤ 100) denoting the number of queries to come. Next q lines contain a node number for which you have to evaluate the expected number of times the node is visited. The last test case has value of zero for n which should not be processed.

Output

Output for each test case should start with “Case #i:” with next q lines containing the results of the queries in the input with three decimal places. There can be situations where a node will be visited forever (for example, an infinite for loop). In such cases, you should print “infinity” (without the quotes). See the sample output section for details of formatting.

Sample Input                                  Output for Sample Input

3

1 2

2 3

2 1

0 0

3

1

2

3

3

1 2

2 3

3 1

0 0

3

3

2

1

  1. 0

Case #1:

2.000

2.000

1.000

Case #2:

infinity

infinity

infinity


Problem setter: Mohammad Sajjad Hossain

Special Thanks: Shahriar Manzoor

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. #include <cmath>
  6. using namespace std;
  7.  
  8. const int maxn=;
  9. const double eps=1e-;
  10. typedef double Matrix[maxn][maxn];
  11. Matrix A;
  12. int n,d[maxn];//d数组存i节点的初读
  13. bool inf[maxn];//标记无穷变量
  14. vector<int> pre[maxn];//存i节点的前驱
  15.  
  16. void swap(double &a,double &b){double t=a;a=b;b=t;}
  17.  
  18. void gauss_jordan()
  19. {
  20. int i,j,r,k;
  21. for(i=;i<n;i++)
  22. {
  23. r=i;
  24. for(j=i+;j<n;j++)
  25. if(fabs(A[j][i])>fabs(A[r][i])) r=j;
  26. if(fabs(A[r][i])<eps) continue;
  27. if(r!=i)for(j=;j<=n;j++) swap(A[r][j],A[i][j]);
  28. //与第i行以外的其他行进行消元
  29. for(k=;k<n;k++) if(k!=i)
  30. for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
  31. }
  32. }
  33. int main()
  34. {
  35. //freopen("in.txt","r",stdin);
  36. //freopen("out.txt","w",stdout);
  37. int i,j,icase=;
  38. while(scanf("%d",&n),n)
  39. {
  40. memset(d,,sizeof(d));
  41. for(i=;i<n;i++) pre[i].clear();
  42. int a,b;
  43. while(scanf("%d %d",&a,&b),a)
  44. {
  45. a--;b--;d[a]++;
  46. pre[b].push_back(a);
  47. }
  48. memset(A,,sizeof(A));
  49. for(i=;i<n;i++)//构造方程组
  50. {
  51. A[i][i]=;
  52. for(j=;j<pre[i].size();j++)
  53. A[i][pre[i][j]]-=1.0/d[pre[i][j]];
  54. if(i==) A[i][n]=;
  55. }
  56. //解方程组,标记无穷变量
  57. gauss_jordan();
  58. memset(inf,,sizeof(inf));
  59. for(i=n-;i>=;i--)
  60. {
  61. if(fabs(A[i][i])<eps && fabs(A[i][n])>eps) inf[i]=true;//这个变量无解,标记为无穷变量
  62. for(j=i+;j<n;j++)//跟无穷变量扯上关系的也是无穷的
  63. if(fabs(A[i][j])>eps && inf[j]) inf[i]=true;
  64. }
  65. int q,p;
  66. scanf("%d",&q);
  67. printf("Case #%d:\n",++icase);
  68. while(q--)
  69. {
  70. scanf("%d",&p);p--;
  71. if(inf[p]) printf("infinity\n");
  72. else printf("%.3lf\n",fabs(A[p][p])<eps?0.0:A[p][n]/A[p][p]);
  73. }
  74. }
  75. return ;
  76. }

uva 10828 高斯消元求数学期望的更多相关文章

  1. 【BZOJ3143】游走(高斯消元,数学期望)

    [BZOJ3143]游走(高斯消元,数学期望) 题面 BZOJ 题解 首先,概率不会直接算... 所以来一个逼近法算概率 这样就可以求出每一条边的概率 随着走的步数的增多,答案越接近 (我卡到\(50 ...

  2. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

  3. hdu 4870 rating(高斯消元求期望)

    Rating Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  4. HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)

    网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...

  5. 【BZOJ2137】submultiple 高斯消元求伯努利数

    [BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...

  6. SPOJ HIGH(生成树计数,高斯消元求行列式)

    HIGH - Highways no tags  In some countries building highways takes a lot of time... Maybe that's bec ...

  7. 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基

    题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...

  8. 【bzoj3105】[cqoi2013]新Nim游戏 高斯消元求线性基

    题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴,但不能同时从 ...

  9. 【bzoj4004】[JLOI2015]装备购买 贪心+高斯消元求线性基

    题目描述 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 (1 <= i <= n; 1 <= j < ...

随机推荐

  1. javaweb基础(17)_jsp九个内置对象

    一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...

  2. 机器学习十大常用算法(CITE 不会停的蜗牛 ) interesting

    算法如下: 决策树 随机森林算法 逻辑回归 SVM 朴素贝叶斯 K最近邻算法 K均值算法 Adaboost 算法 神经网络 马尔可夫 1. 决策树 根据一些 feature 进行分类,每个节点提一个问 ...

  3. pandas实践——美国人口分析

    1.导入文件,并查看数据样本 abbr = pd.read_csv("./state-abbrevs.csv")areas =pd.read_csv("./state-a ...

  4. LeetCode1090. 受标签影响的最大值

    问题: 我们有一个项的集合,其中第 i 项的值为 values[i],标签为 labels[i]. 我们从这些项中选出一个子集 S,这样一来: |S| <= num_wanted 对于任意的标签 ...

  5. 如何使用postman做接口测试

    1.get请求传参 只要是get请求都可以在浏览器中直接发: 在访问地址后面拼  ?key=value&key=value 例如: 在浏览器中直接输入访问地址,后面直接拼需要传给服务器的参数 ...

  6. 使用laravel框架的eloquent\DB模型连接多个数据库

    1.配置.env文件 DB_HOST_TRAILER=127.0.0.1DB_PORT_TRAILER=3306DB_DATABASE_TRAILER=htms_trailerDB_USERNAME_ ...

  7. 【mysql】【转发】my.cnf 讲解

    PS:本配置文件针对Dell R710,双至强E5620.16G内存的硬件配置.CentOS 5.6 64位系统,MySQL 5.5.x 稳定版.适用于日IP 50-100w,PV 100-300w的 ...

  8. ubuntu gcc的下载链接,比较快的。

    http://mirrors.163.com/ubuntu-releases/ http://xhmikosr.1f0.de/tools/msys/

  9. Python中re(正则表达式)模块使用方法

    Python中常用的正则表达式处理函数: re.match re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词. import re text = "JGood ...

  10. 【HIHOCODER 1176】 欧拉路·一

    描述 小Hi和小Ho最近在玩一个解密类的游戏,他们需要控制角色在一片原始丛林里面探险,收集道具,并找到最后的宝藏.现在他们控制的角色来到了一个很大的湖边.湖上有N个小岛(编号1..N),以及连接小岛的 ...