uva 10828 高斯消元求数学期望
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-else, for, do-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 高斯消元求数学期望的更多相关文章
- 【BZOJ3143】游走(高斯消元,数学期望)
[BZOJ3143]游走(高斯消元,数学期望) 题面 BZOJ 题解 首先,概率不会直接算... 所以来一个逼近法算概率 这样就可以求出每一条边的概率 随着走的步数的增多,答案越接近 (我卡到\(50 ...
- HDU4870_Rating_双号从零单排_高斯消元求期望
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...
- hdu 4870 rating(高斯消元求期望)
Rating Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- HDU 5833 (2016大学生网络预选赛) Zhu and 772002(高斯消元求齐次方程的秩)
网络预选赛的题目……比赛的时候没有做上,确实是没啥思路,只知道肯定是整数分解,然后乘起来素数的幂肯定是偶数,然后就不知道该怎么办了… 最后题目要求输出方案数,首先根据题目应该能写出如下齐次方程(从别人 ...
- 【BZOJ2137】submultiple 高斯消元求伯努利数
[BZOJ2137]submultiple Description 设函数g(N)表示N的约数个数.现在给出一个数M,求出所有M的约数x的g(x)的K次方和. Input 第一行输入N,K.N表示M由 ...
- SPOJ HIGH(生成树计数,高斯消元求行列式)
HIGH - Highways no tags In some countries building highways takes a lot of time... Maybe that's bec ...
- 【bzoj2115】[Wc2011] Xor DFS树+高斯消元求线性基
题目描述 输入 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 Di的无向边. 图 ...
- 【bzoj3105】[cqoi2013]新Nim游戏 高斯消元求线性基
题目描述 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以选一个火柴堆拿走若干根火柴.可以只拿一根,也可以拿走整堆火柴,但不能同时从 ...
- 【bzoj4004】[JLOI2015]装备购买 贪心+高斯消元求线性基
题目描述 脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量zi(aj ,.....,am) 表示 (1 <= i <= n; 1 <= j < ...
随机推荐
- c#和Java中的继承
c#和Java: 1.首先,子类继承了父类的属性和方法,但是子类并没有继承父类的私有字段. 2.子类并没有继承父类的构造函数,但是.子类会默认的调用父类无参数的构造函数,创建父类对象,让子类可以使用父 ...
- java基础——反射机制
反射机制是什么 反射机制就是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为jav ...
- 理解AttributeUsage类
类定义: // 摘要: // 指定另一特性类的用法. 此类不能被继承. [Serializable] [AttributeUsage(AttributeTargets.Class, Inherited ...
- 读取Exchange的用户未读邮件数的几种方法
[http://www.cnblogs.com/nbpowerboy/p/3539422.html] 可以使用ExchangeServiceBinding获取邮件,他相当于outlook, 来获取服务 ...
- iOS--UIScrollView基本用法和代理方法
主要是为了记录下UIScrollView的代理方法吧 在帮信息学院的学长做东西的时候需要大量用到分块浏览,所以就涉及到很多的关于scrollview,所以也就有了这篇文章 - (void)view ...
- iOS或Mac开发者应该记住的前缀
- vuePress的使用
今天来玩一玩vuePress的使用,用markdown来编辑一个页面网站,这里谈论到了简单使用,细节可以去官网上去查看 开始安装 项目依赖 // package.json { "name&q ...
- destoon修改手机端分页
1. global.func.php pages函数和listpages函数 函数开头增加 $DT_TOUCH,$newsamplepages变量 global $DT_URL, $DT, $L,$D ...
- 【php】【异步】php实现异步的几种方法
请参考 4种php常用的异步执行方式 ajax 和 img 的 src 属性 系统指令调用 (在php代码里面调用系统指令) curl socket通信
- python-numpy-pandas
目录 numpy 模块 创建矩阵方法: 获取矩阵的行列数 切割矩阵 矩阵元素替换 矩阵的合并 通过函数创建矩阵 矩阵的运算 pandas模块 series (一维列表) DataFrame DataF ...