• 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-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

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <vector>
     5 #include <cmath>
     6 using namespace std;
     7 
     8 const int maxn=110;
     9 const double eps=1e-8;
    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=0;i<n;i++)
    22     {
    23         r=i;
    24         for(j=i+1;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=0;j<=n;j++) swap(A[r][j],A[i][j]);
    28             //与第i行以外的其他行进行消元
    29             for(k=0;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=0;
    38     while(scanf("%d",&n),n)
    39     {
    40         memset(d,0,sizeof(d));
    41         for(i=0;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,0,sizeof(A));
    49         for(i=0;i<n;i++)//构造方程组
    50         {
    51             A[i][i]=1;
    52             for(j=0;j<pre[i].size();j++)
    53                 A[i][pre[i][j]]-=1.0/d[pre[i][j]];
    54             if(i==0) A[i][n]=1;
    55         }
    56         //解方程组,标记无穷变量
    57         gauss_jordan();
    58         memset(inf,0,sizeof(inf));
    59         for(i=n-1;i>=0;i--)
    60         {
    61             if(fabs(A[i][i])<eps && fabs(A[i][n])>eps) inf[i]=true;//这个变量无解,标记为无穷变量
    62             for(j=i+1;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:
    ",++icase);
    68         while(q--)
    69         {
    70             scanf("%d",&p);p--;
    71             if(inf[p]) printf("infinity
    ");
    72             else printf("%.3lf
    ",fabs(A[p][p])<eps?0.0:A[p][n]/A[p][p]);
    73         }
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    java中Condition类的详细介绍(详解)
    Semaphore原理以及使用总结
    RocketMQ消息存储原理总结(一)
    Es Bucket聚合(桶聚合)总结
    "元素隐式具有 “any” 类型,因为类型“Shared”没有索引签名"问题解决思路
    有关生产环境tomcat java应用报错Caused by: java.lang.OutOfMemoryError: Java heap space的调研
    docker 实践02
    正睿比赛总结day1
    【水】一个神秘玩意:如何分辨出差别很小的颜色?
    AtCoder Regular Contest 125 比赛记录(vp)
  • 原文地址:https://www.cnblogs.com/xiong-/p/3861681.html
Copyright © 2020-2023  润新知