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

0

Case #1:

2.000

2.000

1.000

Case #2:

infinity

infinity

infinity


Problem setter: Mohammad Sajjad Hossain

Special Thanks: Shahriar Manzoor

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std; const int maxn=;
const double eps=1e-;
typedef double Matrix[maxn][maxn];
Matrix A;
int n,d[maxn];//d数组存i节点的初读
bool inf[maxn];//标记无穷变量
vector<int> pre[maxn];//存i节点的前驱 void swap(double &a,double &b){double t=a;a=b;b=t;} void gauss_jordan()
{
int i,j,r,k;
for(i=;i<n;i++)
{
r=i;
for(j=i+;j<n;j++)
if(fabs(A[j][i])>fabs(A[r][i])) r=j;
if(fabs(A[r][i])<eps) continue;
if(r!=i)for(j=;j<=n;j++) swap(A[r][j],A[i][j]);
//与第i行以外的其他行进行消元
for(k=;k<n;k++) if(k!=i)
for(j=n;j>=i;j--) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int i,j,icase=;
while(scanf("%d",&n),n)
{
memset(d,,sizeof(d));
for(i=;i<n;i++) pre[i].clear();
int a,b;
while(scanf("%d %d",&a,&b),a)
{
a--;b--;d[a]++;
pre[b].push_back(a);
}
memset(A,,sizeof(A));
for(i=;i<n;i++)//构造方程组
{
A[i][i]=;
for(j=;j<pre[i].size();j++)
A[i][pre[i][j]]-=1.0/d[pre[i][j]];
if(i==) A[i][n]=;
}
//解方程组,标记无穷变量
gauss_jordan();
memset(inf,,sizeof(inf));
for(i=n-;i>=;i--)
{
if(fabs(A[i][i])<eps && fabs(A[i][n])>eps) inf[i]=true;//这个变量无解,标记为无穷变量
for(j=i+;j<n;j++)//跟无穷变量扯上关系的也是无穷的
if(fabs(A[i][j])>eps && inf[j]) inf[i]=true;
}
int q,p;
scanf("%d",&q);
printf("Case #%d:\n",++icase);
while(q--)
{
scanf("%d",&p);p--;
if(inf[p]) printf("infinity\n");
else printf("%.3lf\n",fabs(A[p][p])<eps?0.0:A[p][n]/A[p][p]);
}
}
return ;
}

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. key directories in the linux file system

    Key directories in the file system: */: Root directory (base of file system) /bin: Executable progra ...

  2. SC || Chapter 1

    第一章的重中之重就是这张图吧 (具体参见笔记) ┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉┉∞ ∞┉┉┉∞ ∞┉┉ 区分哪些属性是外部的(面向用户 ...

  3. Bootstrap标签页(Tab)插件事件

    事件 下表列出了标签页(Tab)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 show.bs.tab 该事件在标签页显示时触发,但是必须在新标签页被显示之前.分别使用 even ...

  4. SDWebImage解析

    SDWebImage托管在github上.https://github.com/rs/SDWebImage 这个类库提供一个UIImageView类别以支持加载来自网络的远程图片.具有缓存管理.异步下 ...

  5. 初涉斯坦纳树&&bzoj4774: 修路

    斯坦纳树的基础应用 斯坦纳树有什么用 个人一点粗浅理解…… 最基本形式的斯坦纳树问题(以下简称母问题):给定图G和一个关键点集V.求在G中选取一个权值最小(这里权值可以有很多变式)的边集E使V中的点两 ...

  6. [BZOJ] 1907: 树的路径覆盖

    一个点必然被路径覆盖,根据是否为路径的端点分类 \(f[x][0]\)表示以\(x\)为根的子树,\(x\)不为端点的最小路径覆盖数 \(f[x][1]\)表示以\(x\)为根的子树,\(x\)为一条 ...

  7. PHP函数详解:call_user_func()使用方法

    call_user_func函数类似于一种特别的调用函数的方法,使用方法如下: <?php function nowamagic($a,$b) { echo $a; echo $b; } cal ...

  8. Python学习笔记:输入输出,注释,运算符,变量,数字类型,序列,条件和循环控制,函数,迭代器与生成器,异常处理

    输入输出 输入函数input()和raw_input() 在Python3.x中只有input()作为输入函数,会将输入内容自动转换str类型: 在Python2.x中有input()和raw_inp ...

  9. Tracer Deployment UVALive - 8271 二分图匹配

    复习二分图又想起了这道题,裸的二分图匹配,直接匈牙利算法就可以了,mark一下这个比较好用的稠密图匈牙利算法模板 题目:题目链接 AC代码: #include <iostream> #in ...

  10. Linux学习-检验软件正确性

    md5sum / sha1sum / sha256sum 目前有多种机制可以计算文件的指纹码,我们选择使用较为广泛的 MD5, SHA1 或 SHA256 加密机 制来处理,我们拿NTP 软件来检查看 ...