Electric resistance
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 655 Accepted Submission(s): 346
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!
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为出口
求整个电阻的等价电阻
题解:
每一个节点流入和流出的电流是相等的(基尔霍夫第二定律)
设每个节点的电势为相应的X(i),可以建立n个方程
若节点1流出的电流为1,即X1=-1,则Xn=1,n这个点的电势在数值上等于电阻
return 1表示有解,return 0表示无解
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define MAXN 100
#define mem(a) memset(a,0,sizeof(a))
const double eps=1e-9;
double arr[MAXN][MAXN];
double ans[MAXN];
int equ,var;///方程的个数,变量的个数
int Gauss()
{
int row=0,col=0;
for(;row<equ&&col<var;row++,col++)
{
int max_r=row;
for(int i=row+1;i<equ;i++)
if(fabs(arr[i][col])>fabs(arr[max_r][col]))
max_r=i;
if(fabs(arr[max_r][col])<eps)
return 0;
if(row!=max_r){
for(int j=col;j<var;j++)
swap(arr[row][j],arr[max_r][j]);
swap(ans[row],ans[max_r]);
}
ans[row]/=arr[row][col];
for(int j=col+1;j<var;j++)
arr[row][j]/=arr[row][col];
arr[row][col]=1;
for(int i=0;i<equ;i++){
if(i!=row){
ans[i]-=ans[row]*arr[i][row];
for(int j=col+1;j<var;j++)
arr[i][j]-=arr[row][j]*arr[i][col];
arr[i][col]=0;
}
}
}
return 1;
}
int main()
{
int T;
int n,m;
int a,b,c;
int cases=1;
//freopen("in.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
mem(arr),mem(ans);
scanf("%d%d",&n,&m);
equ=var=n;
for(int i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
arr[b-1][a-1]+=1.0/c;
arr[b-1][b-1]+=-1.0/c;
arr[a-1][b-1]+=1.0/c;
arr[a-1][a-1]+=-1.0/c;
}
for(int i=0;i<n;i++)
arr[n-1][i]=0;
arr[n-1][0]=1;
ans[0]=1,ans[n-1]=0;
Gauss();
printf("Case #%d: %0.2lf\n",cases++,ans[n-1]);
}
return 0;
}