Youmust have heard the nameKernighanAndRitchie, The authorsThe C Programming Language. While coding inC, We use differentcontrol statements and loops, such,If-then-Else,For,Do-while, Etc. Consider the following fragment of pseudo code:
// Execution starts here
Do {
U;
V;
} While (Condition);
W;
Inthe above code, there is a bias in each conditional branch. Such codes can berepresented by control flow graphs like below:
Letthe probability of jumping from one node of the graph to any of its adjacentnodes be equal. so, in the abovecode fragment, the expected number of times U executes is 2. inthis problem, you will be given with such a control flow graph and find theexpected number of times a node is visited starting from a specific node.
Input
Inputconsists of several test cases. there will be maximum 100 test cases. each case starts with an integer: n (n = 100 ). here NIS 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 twointegers: startand endwhich means execution may jump from node start to node end. A value of zero for start endsthis list. after this, there will be an integer Q (Q ≤100) denoting the numberof queries to come. next Q lines contain a node number for which you have to evaluate theexpected number of times the node is visited. the last test case has value ofzero for nwhich shocould not be processed.
Output
Output for each test caseshocould start with "Case # I:" with next Q lines containing the results of the queries in the input with threedecimal places. there can be situations where a node will be visited forever (for example, an infinite forloop ). in such cases, you shoshould print "infinity" (without the quotes ). see thesample 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: Hammad Sajjad Hossain
Special thanks: Shahriar Manzoor
The probability of starting from each node to each subsequent node is equal. After a non-successor node is executed, the whole process stops and the program starts to run from the node numbered 1, your task is to find the expected number of executions of each node for several query points.
Idea: For this question, we can convert it into an equation: xi = XA/DA + XB/DB + XC/DC + ..., if I is set to Di, the expected number of executions is XI, and XA is its precursor. However, this question is different from other questions for solving equations, that is, there may be an infinite number of conflicting equations or redundant equations. Therefore, we need to use Gaussian-isom elimination method to omit the Back-to-generation process, at the same time, it also needs to handle the possibility of infinite solutions.
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>#include <vector>using namespace std;const int maxn = 110;const double eps = 1e-8;typedef double Matrix[maxn][maxn];Matrix A;int n, d[maxn];vector<int> prev[maxn];int inf[maxn];void gauss_jordan(Matrix A, int n) {int i, j, k, r;for (i = 0; i < n; i++) {r = i;for (j = i+1; 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 = 0; j <= n; j++)swap(A[r][j], A[i][j]);for (k = 0; k < n; k++)if (i != k)for (j = n; j >= i; j--)A[k][j] -= A[k][i] / A[i][i] * A[i][j];}}int main() {int cas = 1;while (scanf("%d", &n) != EOF && n) {memset(d, 0, sizeof(d));for (int i = 0; i < n; i++)prev[i].clear();int a, b;while (scanf("%d%d", &a, &b) != EOF && a) {a--, b--;d[a]++;prev[b].push_back(a);}memset(A, 0, sizeof(A));for (int i = 0; i < n; i++) {A[i][i] = 1;for (int j = 0; j < prev[i].size(); j++)A[i][prev[i][j]] -= 1.0 / d[prev[i][j]];if (i == 0)A[i][n] = 1;}gauss_jordan(A, n);memset(inf, 0, sizeof(inf));for (int i = n-1; i >= 0; i--) {if (fabs(A[i][i]) < eps && fabs(A[i][n]) > eps)inf[i] = 1;for (int j = i+1; j < n; j++)if (fabs(A[i][j]) > eps && inf[j])inf[i] = 1;}int q, u;scanf("%d", &q);printf("Case #%d:\n", cas++);while (q--) {scanf("%d", &u);u--;if (inf[u])printf("infinity\n");else printf("%.3lf\n", fabs(A[u][u]) < eps ? 0.0 : A[u][n] / A[u][u]);}}return 0;}
Ultraviolet A-10828 back to kernighan-Ritchie (equation element elimination)