hdu3976--Electric resistance(高斯消元问题7)

Electric resistance

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Submit Status

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 

给出n个节点,和每个节点中的电阻,问总的电阻是多少。

设每个节点的电势为ui那么总的电压为un - u1 , 设流过这个电路的电流为1,那么电阻也就是un - u1了。通过每个节点流入的电流和流出的电流相同,得到n个方程。

设在1节点流入的是-1,n节点流出的是1,那么对于u到v有电阻w,也就是u点的电流 (v-u)/w,对于v点的电流为(u-v)/w ;

得到n个方程后高斯消元,设1节点的电势为0,这样就能直接求出n节点的电势,也就是整个的电阻了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std ;
#define eps 1e-8
double Map[60][60] , a[60] , x[60] ;
void swap1(int p,int q,int n)
{
    int i ;
    double temp ;
    temp = a[p] ; a[p] = a[q] ; a[q] = temp ;
    for(i = 1;  i <= n ; i++)
    {
        temp = Map[p][i] ; Map[p][i] = Map[q][i] ; Map[q][i] = temp ;
    }
    return ;
}
double gauss(int n)
{
    int i , j , k , t , max1 ;
    double temp ;
    for(i = 1 , t = 1 ; i <= n && t <= n ; i++ , t++)
    {
        max1 = i ;
        for(j = i ; j <= n ; j++)
            if( fabs(Map[j][t])-fabs(Map[max1][t]) > 0 )
            {
                max1 = j ;
            }
        if( fabs(Map[max1][t]) < eps )
        {
            i-- ;
            continue ;
        }
        if( i != max1 )
            swap1(i,max1,n) ;
        for(j = i+1 ; j <= n ; j++)
        {
            if( fabs(Map[j][t]) < eps ) continue ;
            temp = Map[j][t] / Map[i][t] ;
            a[j] -= (a[i]*temp) ;
            for(k = t ; k <= n ; k++)
                Map[j][k] -= (Map[i][k]*temp) ;
        }
    }
    for(i = n ; i >= 1 ; i--)
    {
        x[i] = a[i] ;
        for(j = i+1 ; j <= n ; j++)
            x[i] -= ( Map[i][j]*x[j] ) ;
        x[i] /= Map[i][i] ;
    }
    return fabs(a[n-1]/Map[n-1][n]) ;
}
int main()
{
    int t , tt , n , m , u , v ;
    double w ;
    scanf("%d", &t) ;
    for(tt = 1 ; tt <= t ; tt++)
    {
        memset(Map,0,sizeof(Map)) ;
        memset(a,0,sizeof(a)) ;
        scanf("%d %d", &n, &m) ;
        a[1] = -1.0 ;
        a[n] = 1.0 ;
        while(m--)
        {
            scanf("%d %d %lf", &u, &v, &w) ;
            Map[u][u] -= 1.0/w ;
            Map[u][v] += 1.0/w ;
            Map[v][u] += 1.0/w ;
            Map[v][v] -= 1.0/w ;
        }
        for(int i = 1 ; i <= n ; i++)
            Map[i][1] = 0 ;
        printf("Case #%d: %.2lf\n", tt, gauss(n) );
    }
    return 0;
}
时间: 2024-08-28 09:56:09

hdu3976--Electric resistance(高斯消元问题7)的相关文章

hdu3976(Electric resistance) 高斯消元

Electric resistance Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 907    Accepted Submission(s): 521 Problem Description Now give you a circuit who has n nodes (marked from 1 to n) , please te

HDU 3976 Electric resistance (高斯消元)

题目地址:HDU 3976 分别对n个结点建立n个未知数. 下面这段来自kuangbin博客,传送门http://www.cnblogs.com/kuangbin/p/3428573.html 就根据n个点,流入电流等于流出电流,或者说每个点电流之和(假如流入为正,流出为负,反之也可) 这样可以列出n个方程,根据n个点电流和为0. 而且可以假设1这个点流入电流为-1, 这样设点电势为0,那么可以知道n这个点的电势就等于等效电阻了.. 流入肯定等于流出的,上面列的方程组中第n个的是多余的,可以去掉

HDU5006 Resistance(高斯消元)

给你一个复杂的网路图,然后告诉你s,t,求s,t的等效电阻.方法是设s的电势为1,t的电势为0.然后对于其它的每个点x,满足的是sigma(ux-uy)/R(x,y)(即对每个与x相连的节点y,电势差除以电阻的和为0,应该是基尔霍夫定律什么的),然后就列出来了一堆方程,解出每个点的电势,对于源点连出去的所有边,求一下电流,知道总电流,而且也知道总电势,就可以知道电阻了. #include <iostream> #include <cstdio> #include <cstri

(高斯消元)HDU 5006 Resistance 2014 鞍山网赛

题目链接 题意:有一个电路,用0/1的电阻连接起来.给定两点,问之间的电阻为多少? 先回忆一下中学物理知识,若用并联串联去做,碰到复杂电路根本分析不清.这里用到基尔霍夫定理. 在任一瞬时,流向某一结点的电流之和恒等于由该结点流出的电流之和. 在任一瞬间,沿电路中的任一回路绕行一周,在该回路上电动势之和恒等于各电阻上的电压降之和. 那么我们对于图中的点(电阻为0的看作一个点,缩点)都可以列方程  ∑(Ua-Ub)/Rab =0 Rab都是1所以某条边的电流就等于电压差了. 为了方便求结果,我们设S

uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)

题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,任何一点的电流向量为0.就是说有多少电流流入该节点,就有多少电流流出. 对于每次询问的两点间等效电阻,先判断说两点是否联通,不连通的话绝逼是1/0(无穷大).联通的话,将同一个联通分量上的节点都扣出来,假设电势作为变元,然后根据基尔霍夫定律列出方程,因为对于每个节点的电流向量为0,所以每个节点都有一个方程,所有与该节点

2014鞍山网络预选赛1010(缩点+高斯消元)hdu5006

Resistance Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 280    Accepted Submission(s): 82 Problem Description Recently DRD got a number of wires. Some of the wires have the resistance 1 ohm

UVA 10808 - Rational Resistors(高斯消元+并查集+分数+基尔霍夫定律)

UVA 10808 - Rational Resistors 题意:给定一些结点,有一些电阻,电阻分布在边上,给定一个电路图,每次询问两点,求这两点间的等效电阻 思路:根据基尔霍夫定律,任意一点的电流向量为0,这样就能设每个结点的电势,列出方程,利用高斯消元求解,对于无解的情况,肯定是两点不能连通,这个可以利用并查集判断. 此外这题有个很坑的地方啊,就是高斯消元的姿势不够优美就会爆long long 代码: #include <cstdio> #include <cstring>

学习高斯消元

点击打开链接     习题  行列式 高斯消元问题类型: 用LCM 保持整型 1. 基本的高斯消元,裸模板 HDU3359 2. 开关问题,用^操作代替 -, 求x[i]时候一样用*  poj 1222 1830 1753 3. 枚举自由变元, return -1 是因为出现[0,0,0,0,a]这种情况,return 0 是唯一解,否则是有自由变元 4. 取模方程 (a1*x1+a2*x2...)%P=b在初等行变换后,每次进行消元的时候将所有值模P,最后求解回带的时候利用扩展欧几里得来对每一

BZOJ 3270 博物馆 期望DP+高斯消元

题目大意:给定一张无向连通图,两个人初始各在某个点上,每个时刻每个人会不动或任选出边走,求两人最终期望在哪里相遇 把点数平方,原图上的两个点(x,y)变成新图上的一个点 然后令A为这个图的邻接矩阵(若两人在同一点上则没有出边,否则按概率转移),S为初始行向量(S[(a,b)]=1),ans为答案行向量 那么有ans=S+SA+SA^2+SA^3+... =S(I-A^+∞)/(I-A) =S/(I-A) 于是有ans*(I-A)=S 于是对I-A的转置求高斯消元即可. 和驱逐猪猡那题的思路很像.