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 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个结点的电路图,任意两个结点均联通,

任意两点之间最多有一个电阻,求结点1与N之间的等效电阻。

分析:外加电流源求等效电阻,用结点电压法,以结点1为参考结点U0=0,

即把结点1作为电势零点,结点2,3,4,...,N的电势为Un1,Un2,Un3,...,Un(N-1),

一共N-1个未知数,再在结点1与结点N之间加一个电流源I,列结点电压方程,

那么一共可列N-1个独立方程(含N-1个未知数),再用高斯消元解方程(一定有解),

解出Un(N-1),等效电阻R=(Un(N-1)-U0)/I=Un(N-1)/I,为方便计算取I=1.0A,

那么R=Un(N-1).

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
double a[60][60];
void Guass(int N,int M)
{
    for(int i=1;i<=N;i++)//i是列数,一个一个消去
    {//上三角
        if(fabs(a[i][i])<1e-6)//第i行第i个为0
        {//找出i+1到第M行中,第i个数不为0的行
            int p=M;
            for(;p>i;p--)
            if(fabs(a[p][i])>1e-6) break;
            if(p==i) continue;
            else swap(a[i],a[p]);//交换i,p两行
        }
        for(int k=i+1;k<=M;k++)
        {
            for(int j=i+1;j<=N+1;j++)
            {
                a[k][j]-=a[i][j]/a[i][i]*a[k][i];
            }
            a[k][i]=0;
        }
    }
    //从下往上消去
    for(int i=N;i;i--)//第i个解
    {
        for(int k=i-1;k;k--)//消去1到i-1行的第i个元素
        {
            a[k][N+1]-=a[i][N+1]/a[i][i]*a[k][i];
            a[k][i]=0;
        }
    }
}

int main()
{
    int T,cas=0;
    int N,M;//N个结点M条支路
    scanf("%d",&T);
    while(T--)
    {
        memset(a,0,sizeof(a));
        scanf("%d%d",&N,&M);
        for(int i=0;i<M;i++)
        {
            int s,t;double r;
            scanf("%d%d%lf",&s,&t,&r);
            //结点电压法
            if(s==1) a[t-1][t-1]+=1.0/r;
            else if(t==1) a[s-1][s-1]+=1.0/r;
            else
            {
                a[s-1][s-1]+=1.0/r;a[s-1][t-1]-=1.0/r;
                a[t-1][t-1]+=1.0/r;a[t-1][s-1]-=1.0/r;
            }
        }
        a[N-1][N]=1.0;//外加电流源
        Guass(N-1,N-1);//N-1个未知数
        printf("Case #%d: %.2lf\n",++cas,fabs(a[N-1][N]/a[N-1][N-1]));
    }
    return 0;
}

原文地址:https://www.cnblogs.com/ACRykl/p/8711227.html

时间: 2024-08-28 09:56:09

hdu3976(Electric resistance) 高斯消元的相关文章

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个的是多余的,可以去掉

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 bet

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的转置求高斯消元即可. 和驱逐猪猡那题的思路很像.