Evaluate the resistance between two points of a general circuit-Gaussian elimination method

Source: Internet
Author: User

Description: A circuit connected with n knots and m resistors is provided in the form of a weighted graph. The resistance between point 1 and point n is obtained. Problem Analysis: This kind of problem occurs in the provincial training team! No. The solution is based on two facts: 1. <kierhov's law>: The total inflow of current at all points equals to the total outflow (except 1 and n ). 2. <Ohm's law>: Due to poor current direction, I = U/R = (Ex-Ey)/R may make the current positive and negative, then Law 1 can be expressed as "the sum of total outbound traffic is equal to 0". Therefore, an equation is listed for each node, and Gaussian elimination can be obtained. There are several noteworthy points: 1. The self-ring is directly ignored, and the duplicate edge is merged using the reciprocal and formula. 2. Find the largest absolute value of the coefficient of Gaussian describe every time, so that the ratio obtained by Division is as small as possible. This ensures that the equations can be solved with high accuracy. (The smaller the floating point number, the higher the precision (see the Code for specific implementation) Code: [cpp] # include <cstdio> # include <cstring> using namespace std; # define abs (_) ({double __= _; (__> 0 )? __:-__;}) Int n, m, u, v, w, a [200] [200]; double B [200] [200]; double now, tot, gs [200] [200], e [200], ans = 0; void gauss () {int c, t, I, j; double k, tmp; for (c = 1; c <= n; c ++) {t = c; for (I = c + 1; I <= n; I ++) if (abs (gs [I] [c])> abs (gs [t] [c]) t = I; for (j = 0; j <= n; j ++) tmp = gs [c] [j], gs [c] [j] = gs [t] [j], gs [t] [j] = tmp; for (I = c + 1; I <= n; I ++) {k = gs [I] [c]/gs [c] [c]; for (j = 0; j <= n; j ++) gs [I] [j]-= gs [c] [j] * k ;}} for (c = n; C> = 1; c --) {tmp = gs [c] [0]; for (j = c + 1; j <= n; j ++) tmp-= gs [c] [j] * e [j]; e [c] = tmp/gs [c] [c] ;}} int main () {int I, j; freopen ("resistor. in "," r ", stdin); freopen (" resistor. out "," w ", stdout); for (;) {if (scanf (" % d ", & n, & m) =-1) break; memset (gs, 0, sizeof (gs); memset (a, 0, sizeof (a); memset (B, 0, sizeof (B); memset (e, 0, sizeof (e); for (I = 1; I <= m; I ++) {scanf ("% d", & u, & v, & w); if (u = v) continu E; if (! A [u] [v]) a [u] [v] = a [v] [u] = 1, B [u] [v] = B [v] [u] = w; else B [u] [v] = 1.0/(1.0/w + 1.0/B [u] [v]), B [v] [u] = B [u] [v] ;}for (I = 2; I <n; I ++) {tot = 0; for (j = 1; j <= n; j ++) if (a [I] [j]) {now = 1.0/B [I] [j]; gs [I] [j] = now, tot-= now;} gs [I] [I] = tot;} gs [1] [0] = 1, gs [1] [1] = 1; gs [n] [0] = 0, gs [n] [n] = 1; gauss (); ans = 0; for (j = 1; j <= n; j ++) if (a [1] [j]) ans + = (1-e [j]) /B [1] [j]; printf ("%. 2lf \ n ", (double) 1.0/ans);} 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.