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 ;
}

最新文章

  1. .Net中的AOP系列之《拦截位置》
  2. iOS 不规则的ImageView
  3. struts 拦截器 Interceptor
  4. CSS实现限制显示的字数,超出显示&quot;...&quot;
  5. MYSQL INSERT INTO SELECT 不插入重复数据
  6. SDP协议分析
  7. Windows Server 2003 SP2 R2 企业版/标准版/32与64位 CD-KEY
  8. Android学习笔记--获取传感器信息
  9. Practical Common Lisp
  10. GlusterFS常用命令小结
  11. POJ 2479 Maximum sum 解题报告
  12. mysql备份并转移数据
  13. 干了这碗鸡汤:从理发店小弟到阿里P10技术大牛
  14. Future FutrueTask Callable类源码说明以及原理使用
  15. 7 Make vs Do
  16. 使用PHPStorm 配置自定义的Apache与PHP环境
  17. 076 Apache的HBase与cdh的sqoop集成(不建议不同版本之间的集成)
  18. 【Java】【集合】
  19. Typescript学习总结1
  20. 精选20个高品质的免费素材,可以下载PSD格式

热门文章

  1. 已知一棵完全二叉树,求其节点的个数 要求:时间复杂度低于O(N),N为这棵树的节点个数
  2. PAT (Basic Level) Practise (中文)- 1013. 数素数 (20)
  3. cocos2dx 单张图片加密
  4. JS - 生成UUID
  5. .Net Core依赖注入中TryAddEnumerable 和TryAddTransient方法的区别
  6. BFS:UVa1590-IP Networks (子网掩码相关知识)
  7. POJ 3057 网络流 Evacuation
  8. Python虚拟机函数机制之位置参数(四)
  9. 如何理解redo和undo的作用
  10. “万恶”的break