UVa 10828 back to Kernighan-ritchie Gaussian elimination + probability DP

Source: Internet
Author: User
Tags cas

Topic Source: UVa 10828 back to Kernighan-ritchie

Test instructions: Starting from 1 each time the equal probability from one point to the point of his neighbor has to go to not go stop seeking to stop the expectation of each point

Idea: To write the equation elimination equation has a unique solution to many solutions without solution of the ring has been in the loop can not stop the calculation infinity from 1 can not reach the point expected to be 0

#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath&
Gt
using namespace Std;
const int MAXN = 110;
Const double EPS = 1e-8;
typedef double MATRIX[MAXN][MAXN];
Vector <int> G[MAXN];
int D[MAXN];
int INF[MAXN];
	void Gauss_jordan (Matrix A, int n) {int I, J, R, K;
		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) {if (r! = i) for (j = 0; J <= N; j + +) Swap (A[r][j], a[i][j]); for (k = 0; k < n; k++) {if (k! = i) {for (j = n; j >= i; j--) {a[k][j]-= a[k][i]/a[i][
					I] * a[i][j];
}}}}}} Matrix A;
	int main () {int cas = 1;
	int n;
		while (scanf ("%d", &n) && N) {memset (d, 0, sizeof (d));
		for (int i = 0; i < n; i++) g[i].clear ();
		int u, v; while (scanf ("%d%d", &u, &v) && (u| |
			V)) {u--;
			v--; G[v].push_bacK (U);
		d[u]++;
		} memset (A, 0, sizeof (a));
			for (int i = 0; i < n; i++) {a[i][i] = 1;
			for (int j = 0; J < G[i].size (); j + +) A[i][g[i][j]]-= 1.0/d[g[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;
		scanf ("%d", &q);
		printf ("Case #%d:\n", cas++);
			while (q--) {int x;
			scanf ("%d", &x);
			x--;
				if (Inf[x]) {puts ("infinity");
			Continue
				} if (Fabs (A[x][x]) < EPS) {puts ("0.000");
			Continue
		} printf ("%.3lf\n", A[x][n]/a[x][x]);
}} return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.