hdu 3976 Electric resistance(高斯消元)

来源:互联网 发布:国际常用期货交易软件 编辑:程序博客网 时间:2024/05/21 15:47

Electric resistance

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


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个节点 m对节点之间有电阻 求出节点1到节点n之间的等效电阻

根据一个节点的出电流等于入电流 所以跟一个节点有关的所有电流相加和为0

假设每个节点的电视Ui Σ((u[j]-u[i])/r[i][j])=0

此外假设进入节点1的电流为1 则出节点n的电流为1

两个点之间的等效电阻=两个节点直接的电势差

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 100#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;int Read(){    char c = getchar();    while (c < '0' || c > '9') c = getchar();    int x = 0;    while (c >= '0' && c <= '9') {        x = x * 10 + c - '0';        c = getchar();    }    return x;}void Print(int a){     if(a>9)         Print(a/10);     putchar(a%10+'0');}double a[MAXN][MAXN],x[MAXN];int equ,var;//返回0表示无解 1表示有解int Gauss(){    int i,j,k,col,max_r;    for(k=0,col=0;k<equ&&col<var;k++,col++)    {        max_r=k;        for(i=k+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(k!=max_r)        {            for(j=col;j<var;j++)                swap(a[k][j],a[max_r][j]);            swap(x[k],x[max_r]);        }        x[k]/=a[k][col];        for(j=col+1;j<var;j++)            a[k][j]/=a[k][col];        a[k][col]=1;        for(i=0;i<equ;i++)            if(i!=k)        {            x[i]-=x[k]*a[i][col];            for(j=col+1;j<var;j++)                a[i][j]-=a[k][j]*a[i][col];            a[i][col]=0;        }    }    return 1;}int main(){//    fread;    int tc;    scanf("%d",&tc);    int cs=1;    while(tc--)    {        int n,m;        scanf("%d%d",&n,&m);        MEM(a,0);        while(m--)        {            int u,v;            double w;            scanf("%d%d%lf",&u,&v,&w);            u--; v--;            a[u][v]+=1.0/w;            a[v][u]+=1.0/w;            a[u][u]-=1.0/w;            a[v][v]-=1.0/w;        }//        for(int i = 0;i < n-1;i++)//             x[i] = 0;//         x[0] = 1;//         for(int i = 0;i < n;i++)//             a[n-1][i] = 0;//         x[n-1] = 0;//         a[n-1][0] = 1;//        a[0][n]=-1; a[n-1][n]=1;        MEM(x,0);        x[0]=1; x[n-1]=-1;        equ=var=n;        Gauss();//        for(int i=0;i<n;i++)//            printf("%.2lf ",x[i]);        printf("Case #%d: %.2lf\n",cs++,x[n-1]-x[0]);    }    return 0;}

 




0 0
原创粉丝点击