HDU 3976 Electric resistance(高斯消元)

一看图就233了啊,电路这种高大上的东东,简直了啊。

这是binshen写的,看的他的博客:http://www.cnblogs.com/kuangbin/p/3428573.html

需要列n个方程:
就根据n个点,流入电流等于流出电流,或者说每个点电流之和(假如流入为正,流出为负,反之也可)
这样可以列出n个方程,根据n个点电流和为0.
而且可以假设1这个点流入电流为-1, 这样设点电势为0,那么可以知道n这个点的电势就等于等效电阻了、。
流入肯定等于流出的,上面列的方程组中第n个的是多余的,可以去掉,替换成1点电压为0.
这样方程组正确建立。
对于u  ---->  v  电阻为w.   可以知道u加一个电流  xv/w - xu/w.  而v加一个电流 xu/w - xv/w;    

然后就找到了关系方程组。

可以边化简,边解方程。

Electric resistance

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 377    Accepted Submission(s): 183


Problem Description
Now give you a circuit who has n nodes (marked from 1 to n) , please tell abcdxyzk the equivalent resistance of the circuit between node 1 and node n. You may assume that the circuit is connected. The equivalent resistance of the circuit between 1 and n is that, if you only consider node 1 as positive pole and node n as cathode , all the circuit could be regard as one resistance . (It's important to analyse complicated circuit ) At most one resistance will between any two nodes.
 

Input
In the first line has one integer T indicates the number of test cases. (T <= 100)

Each test first line contain two number n m(1<n<=50,0<m<=2000), n is the number of nodes, m is the number of resistances.Then follow m lines ,each line contains three integers a b c, which means there is one resistance between node a and node b whose resistance is c. (1 <= a,b<= n, 1<=c<=10^4) You may assume that any two nodes are connected!
 

Output
for each test output one line, print "Case #idx: " first where idx is the case number start from 1, the the equivalent resistance of the circuit between 1 and n. Please output the answer for 2 digital after the decimal point .
 

Sample Input
  
  
1 4 5 1 2 1 2 4 4 1 3 8 3 4 19 2 3 12
 

Sample Output
  
  
Case #1: 4.21
 

Author
abcdxyzk
 

Source
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
#define INF 0x7ffffff
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 2

const int maxn = 210;

using namespace std;

double a[maxn][maxn];
double x[maxn];
int equ, var;
int Gauss()
{
    int row, col;
    int max_r;
    row = col = 0;
    while(row < equ && col < var)
    {
        max_r = row;
        for(int i = row+1; i < equ; i++)
            if(fabs(a[i][col]) > fabs(a[max_r][col])) max_r = i;
        if(fabs(a[max_r][col]) < eps) return 0;
        if(row != max_r)
        {
            for(int j = col; j < var; j++) swap(a[row][j], a[max_r][j]);
            swap(x[max_r], x[row]);
        }
        x[row] /= a[row][col];
        for(int j = col+1; j < var; j++) a[row][j] /= a[row][col];
        a[row][col] = 1;
        for(int i = row+1; i < equ; i++)
        {
            x[i] -= x[row]*a[i][row];
            for(int j = col+1; j < var; j++) a[i][j] -= a[row][j]*a[i][col];
            a[i][col] = 0;
        }
        col++;
        row++;
    }
    return 1;
}

int main()
{
    int T;
    int Case = 1;
    cin >>T;
    while(T--)
    {
        int n, m;
        cin >>n>>m;
        memset(a, 0, sizeof(a));
        memset(x, 0, sizeof(x));
        int u, v, w;
        for(int i = 0; i < m; i++)
        {
            cin >>u>>v>>w;
            a[u-1][u-1] += -1.0/w;
            a[v-1][v-1] += -1.0/w;
            a[u-1][v-1] += 1.0/w;
            a[v-1][u-1] += 1.0/w;
        }
        x[0] = 1;
        for(int i = 0; i < n; i++) a[n-1][i] = 0;
        x[n-1] = 0;
        a[n-1][0] = 1;
        equ = var = n;
        Gauss();
        cout<<"Case #"<<Case++<<": ";
        printf("%.2lf\n",x[n-1]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值