【UVA】【10828】随机程序

数学期望/高斯消元/马尔可夫过程

  刘汝佳老师白书上的例题- -b

  本体不满足拓扑关系,但马尔可夫过程是可以高斯消元解的……

  用「高斯·约当消元」更方便!

 1 //UVA 10828
 2 #include<cmath>
 3 #include<vector>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<iostream>
 8 #include<algorithm>
 9 #define rep(i,n) for(int i=0;i<n;++i)
10 #define F(i,j,n) for(int i=j;i<=n;++i)
11 #define D(i,j,n) for(int i=j;i>=n;--i)
12 #define pb push_back
13 using namespace std;
14 int getint(){
15     int v=0,sign=1; char ch=getchar();
16     while(ch<‘0‘||ch>‘9‘){ if (ch==‘-‘) sign=-1; ch=getchar();}
17     while(ch>=‘0‘&&ch<=‘9‘){ v=v*10+ch-‘0‘; ch=getchar();}
18     return v*=sign;
19 }
20 const int N=110;
21 const double eps=1e-8;
22 typedef double Matrix[N][N];
23 /******************tamplate*********************/
24
25 void gauss_jordan(Matrix A,int n){
26     int i,j,k,r;
27     rep(i,n){
28         r=i;
29         for(j=i+1;j<n;++j)
30             if (fabs(A[j][i]) > fabs(A[r][i])) r=j;
31         if (fabs(A[r][i]) < eps)continue;
32         if (r!=i) F(j,0,n) swap(A[r][j],A[i][j]);
33
34         rep(k,n) if (k!=i)
35             D(j,n,i) A[k][j]-=A[k][i]/A[i][i]*A[i][j];
36     }
37 }
38 Matrix A;
39 int n,d[N];
40 vector<int> pre[N];
41 bool inf[N];
42
43 int main(){
44 #ifndef ONLINE_JUDGE
45     freopen("10828.in","r",stdin);
46     freopen("10828.out","w",stdout);
47 #endif
48     int cs=0;
49     while(scanf("%d",&n)==1 && n){
50         memset(d,0,sizeof d);
51         rep(i,n) pre[i].clear();
52         int a,b;
53         while(scanf("%d%d",&a,&b)==2 && a && b){
54             a--; b--;
55             d[a]++;
56             pre[b].pb(a);
57         }
58         memset(A,0,sizeof (A));
59         rep(i,n){
60             A[i][i]=1;
61             rep(j,pre[i].size())
62                 A[i][pre[i][j]]-=1.0/d[pre[i][j]];
63             if (i==0) A[i][n]=1;
64         }
65
66         gauss_jordan(A,n);
67         memset(inf,0,sizeof inf);
68         for(int i=n-1;i>=0;i--){
69             if ( fabs(A[i][i])<eps && fabs(A[i][n])>eps ) inf[i]=1;
70             for(int j=i+1;j<n;j++)
71                 if (fabs(A[i][j])>eps && inf[j]) inf[i]=1;
72         }
73
74         int q,u;
75         scanf("%d",&q);
76         printf("Case #%d:\n",++cs);
77         while(q--){
78             scanf("%d",&u); u--;
79             if (inf[u]) printf("infinity\n");
80             else printf("%.3lf\n",fabs(A[u][u]) < eps ? 0.0 : A[u][n]/A[u][u]);
81         }
82     }
83     return 0;
84 }

时间: 2024-08-10 15:02:17

【UVA】【10828】随机程序的相关文章

UVa 10828 Back to Kernighan-Ritchie 高斯消元+概率DP

题目来源:UVa 10828 Back to Kernighan-Ritchie 题意:从1开始 每次等概率从一个点到和他相邻的点 有向 走到不能走停止 求停止时每个点的期望 思路:写出方程消元 方程有唯一解 多解 无解的情况 有环 一直再环里无法停止算无穷大 从1不能到的点期望为0 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <

uva 10828 高斯消元求数学期望

Back to Kernighan-RitchieInput: 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-the

UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)

UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的执行期望 思路:高斯消元,每个结点的期望等于所有前趋结点的期望/出度的和,由于存在无限循环的情况,不能直接递推,利用高斯消元去做,判断无解的情况既为无限循环,注意如果一个式自xi为0,但是xn也为0,xi值应该是0,表示无法到达 代码: #include <cstdio> #include <cstring

UVA - 10828 Back to Kernighan-Ritchie (方程消元)

Youmust have heard the name of Kernighan and Ritchie, the authors ofThe C Programming Language. While coding in C, we use differentcontrol statements and loops, such as, if-then-else, for, do-while,etc. Consider the following fragment of pseudo code:

UVA 10828 Back to Kernighan-Ritchie(高斯消元)

高斯消元求概率 对于非起点,期望x[i] = ∑x[j] / deg[j] #include<cstdio> #include<iostream> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<map> #include<queue> #include<vector> #includ

【期望DP+高斯消元】 UVA 10828 Back to Kernighan-Ritchie

通道 题意:从1开始 每次等概率从一个点到和他相邻的点 有向 走到不能走停止 求停止时每个点的期望

模仿开奖应用程序

闲来无趣,自己学习了Timer控件的使用,利用Timer控件制作了一个开奖随机程序,代码很简单,实现更简单,主要练习下Timer控件的几个属性和方法. 下面介绍步骤: 1.创建一个Winform应用程序,拖动两个Button按钮和三个Label标签: 2.修改控件的名称,并将Label标签的AutoSizeMode设置为None: 各个控件的代码: 开始摇奖中: ///启动计时器 private void button1_Click(object sender, EventArgs e) { /

wenbao与矩阵

矩阵满足分配率和结合律(非常重要) ****但是不满足交换律 循环矩阵: 两个循环矩阵乘积依然是循环矩阵 矩阵乘法 1 struct mat{ 2 int n, m; 3 double data[MAXN][MAXN]; 4 }; 5 6 int mul(mat& c, const mat& a, const mat& b){ 7 int i, j, k; 8 if (a.m != b.n) 9 return 0; 10 c.n = a.n; 11 c.m = b.m; 12 fo

wenbao与高斯消元

消元  高斯消元 1 typedef double Matrix[maxn][maxn]; 2 void gauss_elimination(Matrix A, int n){ 3 int i, j, k, r; 4 //消元过程 5 for(i = 0; i < n; ++i){ 6 //选一行r并与i行交换 7 r = i; 8 for(j = i+1; j < n; ++j){ 9 if(fabs(A[j][i]) > fabs(A[r][i])) r = j; 10 } 11 i