做点小贡献……基尔霍夫求解电路
物理吧
全部回复
仅看楼主
level 11
1L度
2012年08月04日 03点08分 1
level 11
基尔霍夫方程组应属于最一般求解电路的方法了。
它的原理如下:
对于图G=(V,E)
流量守恒(|V|-1)个独立方程
环电势积分等于非静电力积分(|E|-|V|+1)个独立方程
对于求独立的环方程,我们有如下算法:
求出图G的生成树
PS:生成树的定义为,任意两点连通且有且仅有一条路径
对于求生成树,我们可以用Kruskal算法
Kruskal算法为:枚举每条边,边上两个点如果在一个连通块中,那么就不把这条边放入树中,否则将边放入树中,它连接的两个点所在连通块成为一个新的大连通块。
接着,对于非树边,在树上找到唯一路径,再和非树边连在一起成为一个环
下一楼就开始贴程序了
2012年08月04日 03点08分 2
level 11
MainProgram.h
/*
Program Section: MainProgram
Source: 人for我我for人
Use: 主程序
*/
#pragma once
#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define down(i,b,a) for(int i=b;i>=a;i--)
const double eps = 1e-9,inf = 1e20;
double eq(double a,double b);//实数判等
2012年08月04日 03点08分 3
level 11
LinearEquation.h
/*
Program Section: LinearEquation
Source: 人for我我for人
Use: 解决线性方程组
*/
#pragma once
const int maxdat = 100;
class Equation{
protected:
double cof[maxdat+1][maxdat+2];//系数矩阵
int n;//未知数、方程个数
void rowswap(int r1,int r2);//交换行
void eleminate(int piv,int rt);//消元
bool solveequation();//解方程
public:
void initialize(int _n);//初始化
void cofset(int r,int c,double _cof);//设置系数(重置)
void cofplus(int r,int c,double _delta);//设置系数(加)
bool getans(double *output);//答案输出
};
2012年08月04日 03点08分 5
level 10
mark
2012年08月04日 03点08分 6
level 11
LinearEquation.cpp
/*
Program Section: LinearEquation
Source: 人for我我for人
Use: 解决线性方程组
*/
#include "MainProgram.h"
#include "LinearEquation.h"
void Equation::initialize(int _n){
n = _n;
rep(r,1,n) rep(c,1,n+1) cof[r][c] = 0;//系数设零
}
void Equation::cofset(int r,int c,double _cof){cof[r][c] = _cof;}
void Equation::cofplus(int r,int c,double _delta){cof[r][c] += _delta;}
bool Equation::getans(double *output){
if(!solveequation()) return false;
rep(r,1,n) output[r] = cof[r][n+1];return true;//输出
}
void Equation::rowswap(int r1,int r2){rep(c,1,n+1) swap(cof[r1][c],cof[r2][c]);}
void Equation::eleminate(int piv,int rt){//消元
double multi = cof[rt][piv]/cof[piv][piv];
rep(c,1,n+1) cof[rt][c] -= cof[piv][c]*multi;
}
bool Equation::solveequation(){
rep(r,1,n){
if(eq(cof[r][r],0))//取得主元
rep(rp,r+1,n) if(!eq(cof[rp][r],0)) rowswap(r,rp);
if(eq(cof[r][r],0)) return false;//判断是否满秩
rep(rp,1,n) if(rp!=r) eleminate(r,rp);
}rep(r,1,n) cof[r][n+1]/=cof[r][r],cof[r][r] = 1;//系数化为1
return true;
}

2012年08月04日 03点08分 7
level 11
EletricGraph.h
/*
Program Section: EletricGraph
Source: 人for我我for人
Use: 取得基尔霍夫方程
*/
#pragma once
#include "LinearEquation.h"
namespace data{
const int maxn = 100,geps = 1;
struct edge{
int x,y;
double R,U;
bool ing;
};
extern edge edges[maxn+geps];
void initialize(int _V);
void add(int x,int y,double R,double U);
void getequation(Equation *Eq);
}
2012年08月04日 03点08分 8
level 11
测试数据1
如下图
2012年08月04日 03点08分 10
level 11
这个程序对拍真心难写……
数据也不好出……
但一般样例过了和几个自己手出的数据过了,应该没什么问题……
2012年08月04日 03点08分 11
level 12
我觉得没啥意义
2012年08月04日 04点08分 12
level 12
c。。。
2012年08月04日 04点08分 13
是C++……
2012年08月04日 06点08分
level 13
壮哉我大computer science[啊!]
2012年08月04日 05点08分 14
level 11
[汗]看这语法,C++?
2012年08月04日 06点08分 15
C++完全嵌套C,也就是说……我只是没用到而已……
2012年08月04日 06点08分
回复 @人For我我For人 :物吧人才辈出啊[啊!]
2012年08月04日 06点08分
回复 @金牌松子 :9494
2012年08月04日 06点08分
level 10
[顶]
2012年08月04日 06点08分 16
level 12
路过……好厉害……
2012年08月04日 07点08分 17
level 12
代码收下了,等我回家去玩玩……
2012年08月04日 07点08分 18
level 4
C++是大几学的啊,看样子语句不是一般的多啊[啊!]
2012年08月04日 07点08分 19
不知道……我准高二~OIer
2012年08月04日 09点08分
回复 @人For我我For人 :and PHOer
2012年08月04日 09点08分
回复 @人For我我For人 :哦,难怪学了那么多啊
2012年08月04日 13点08分
level 11
[啊!]只会用VB写简单程序的跪
2012年08月04日 13点08分 20
1