# Museum (comparison of Gauss-Jordan/Gauss-4x constant)

Source: Internet
Author: User

[Question 1] Museum (museum. CPP/C/PAS)

Liu dingfeng, Xiamen No. 1 Middle School

Input File Name: museum. In output file name: museum. Out SourceProgramFile Name: museum. c/CPP/PAS

Time Limit: 4 s memory limit: 128 MB

[Description]

One day, Petya and his friend Vasya made a trip on their numerous trips. They decided to visit a castle Museum. The museum has a special style. It contains N rooms connected by M corridor, and can be accommodated from any room to any other room.
After two people visited the museum for a while, they decided to split their heads to see the art they were interested in. They agreed to join a room at six o'clock P.M. But they forgot one important thing: They didn't choose where to meet. At, they started to run around in the museum to find the other party (they could not call the other party because the roaming fee is very expensive)
However, even though they are running around, they haven't read enough work of art, so each of them takes the following action: make decisions every minute where to go, some probability that they will not go to other places (that is, stay in the room) within this minute, and some probability that they will choose a room in the adjacent room and move along the corridor. Here I refers to the serial number of the current room. Building in ancient times was a very costly task, so each corridor would connect two different rooms and any two rooms would be connected by at most one corridor.
The two boys acted simultaneously. Because the corridor is dark, they cannot meet in the corridor, but they can pass through the corridor in two directions. (In addition, two boys can pass through the same corridor at the same time but will not meet each other) the two boys follow the above steps until they meet each other. Furthermore, when two people choose to go to the same room at a certain time, they will meet in that room.
The two boys are now in rooms A and B respectively, asking for the probability that they will encounter each other in each room.

[Input format]

The first line contains four integers. N indicates the number of rooms. M indicates the number of corridors. A and B (1 ≤ a, B ≤ n) indicate the initial positions of the two boys.
Each row in the next M line contains two integers, indicating the two rooms connected to the corridor.
Each row in N Rows has a real number that is at most accurate to the fourth digit after the decimal point, indicating the probability of waiting in each room.
Ensure that every room is accessible from any other room through the corridor.

[Output format]

The output line contains N numbers separated by spaces. the I-th number indicates the probability that two people will meet in the I-th room (the output retains 6 decimal places)

[Sample input]

2 11 2
1 2
0.5
0.5

[Sample output]

0.5000000.500000

[Data scale]

N <= 5 for 30% of data

N <= 15 for 60% of the data

For 100% of data, n <= 20, n-1 <= m <= N (n-1)/2

[Question source]

Codeforces113d

 

At a glance, the Gauss element ......

Yes, that's true.

The typical path probability problem isSpecify the probability of each point to the end.

But this question does not end ...... Therefore

Let's assume that the end point t

So PT, T = 1

Pi, I = 0 (I = T)

Pi, j = Σ PX, y * g (I, x) * g (J, Y) // G (I, j) indicates the probability of step 1 from X-> I, g (I, I) = PI ...... Self-Calculation

Then enumerate T and obtain O (N * (N ^ 2) ^ 3) gauss_jordan practices.


Haha ......

Let's look at the simple Gauss


Hello ...... This is not scientific ......

Well, we have to admit that Gauss is indeed faster than + Jordan (Jordan's programming complexity is low ......)

The following program uses two methods to change functions.

 

# Include <cstdio> # include <cstring> # include <cstdlib> # include <algorithm> # include <iostream> # include <cmath> using namespace STD; # define for (I, n) for (INT I = 1; I <= N; I ++) # define fork (I, k, n) for (INT I = K; I <= N; I ++) # define rep (I, n) for (INT I = 0; I <n; I ++) # define ord (I, j) ord [I] [J] # define maxn (20 + 3) # define EPS (1e-10) Enum {maxm = maxn * maxn}; int n, m, outdegree [maxn] = {0}; int ord [maxn] [maxn]; void calc_ord () {for (I, n) (J, n) Ord [I] [J] = (I-1) * n + J;} bool map [maxn] [maxn] = {0}; Double P [maxn]; double mat [maxm] [maxm * 2], G [maxn] [maxn]; double F [maxm] [maxm * 2], TMP [maxm * 2]; void print () {for (I, N * n) {for (J, N * n + 1) printf ("%. 2lf ", F [I] [J]); cout <Endl ;}cout <Endl ;}void Gauss (INT N) {memcpy (F, mat, sizeof (f); For (I, n) {// print (); int P = I; // fork (J, I + 1, N * n) if (FABS (F [J] [I])> FABS (F [p] [I]) P = J; while (FABS (F [p] [I]) <EPS & P <n) P ++; If (FABS (F [p] [I]) <EPS) continue; If (P ^ I) {copy (F [p] + 1, F [p] + 1 + n + 1, TMP + 1 ); copy (F [I] + 1, F [I] + 1 + n + 1, F [p] + 1); copy (TMP + 1, TMP + 1 + n + 1, F [I] + 1);} // print (); For (J, n) {if (I = J) continue; double P2 = f [J] [I]/f [I] [I]; // cout <"P =" <p <Endl; For (K, n + 1) f [J] [k]-= f [I] [k] * P2;} // for (I, N * n) f [I] [N * n + 1]/= f [I] [I], F [I] [I] = 1; // print ();} void gauss_no_yuedan (int n) {memcpy (F, mat, sizeof (f); For (INT I = 1; I <= N; I ++) {int Pos; for (Pos = I; POS <n & ABS (F [POS] [I]) <= EPS; POS ++); If (Pos! = I) {copy (F [I] + 1, F [I] + n + 2, TMP + 1); copy (F [POS], f [POS] + n + 2, F [I] + 1); copy (TMP + 1, TMP + n + 2, F [POS] + 1 );} for (Int J = I + 1; j <= N; j ++) {double Mul = (F [J] [I]/f [I] [I]); for (int K = I; k <= n + 1; k ++) f [J] [k]-= f [I] [k] * Mul ;}} // print (); For (INT I = N; I --) {fork (J, I + 1, n) f [I] [n + 1]-= f [I] [J] * f [J] [n + 1], F [I] [J] = 0; if (F [I] [I]! = 0) f [I] [n + 1]/= f [I] [I], F [I] [I] = 1 ;}// print ();} int main () {freopen ("museum. in "," r ", stdin); freopen (" museum. out "," W ", stdout); int X, Y; CIN> N> m> x> Y; calc_ord (); For (I, m) {int U, V; scanf ("% d", & U, & V); map [u] [v] = map [v] [u] = 1; outdegree [v] ++; outdegree [u] ++;} For (I, n) scanf ("% lf", & P [I]), map [I] [I] = 1, G [I] [I] = P [I]; for (I, n) for (J, n) if (I ^ J & map [I] [J]) g [I] [J] = (1-p [I])/(double) outdegree [I]; for (I, n) for (J, n) if (I ^ J) {for (x, n) for (Y, n) if (Map [I] [x] & map [J] [Y]) mat [ord (I, j)] [ord (x, y)] = G [I] [x] * g [J] [Y]; else mat [ord (I, j)] [ord (x, y)] = 0; mat [ord (I, j)] [ord (I, j)]-= 1;} else mat [ord (I, I)] [ord (I, I)] = 1; for (I, N * n) mat [I] [N * n + 1] = 0; // for (I, N * n) mat [I] [N * n + I] = 1; for (I, n) {mat [ord (I, I)] [N * n + 1] = 1; // print (); gauss_no_yuedan (N * n); MAT [ord (I, I)] [N * n + 1] = 0; printf ("%. 6lf ", F [ord (x, y)] [N * n + 1]/f [ord (x, y)] [ord (x, y)]); // if (I ^ N) printf (""); else puts ("");} puts (""); Return 0 ;}

PS: Positive Solution O (N ^ 6). The method is to change the uncertain equations on the left of the equation to E1 * X1 + e2 * X2 +... + en * XN

 

Ex indicates the case of X ...... Ex is the X entry of the sitting edge coefficient matrix.


 

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.