---恢复内容开始---

Resistance
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 1289   Accepted: 418

Description

H.L. is preparing a circuit for the next coming physical experiment. His circuit consists of N nodes, numbered 1 to N, which are connected by wires with certain resistance. H.L is curious about the equivalent resistance between Node 1 and Node N.

Input

The first line contains two positive integers N and M, the number of nodes and wires in the circuit.( N, M ≤ 100)
The next M lines, each describe a wire connection by three integers X, Y, R which indicates that between Node X and Node Y, there is a wire with resistance of R ohm.

Output

The equivalent resistance rounded after the second decimal place.

Sample Input

2 2
1 2 1
1 2 1

Sample Output

0.50

题意:有N个节点,M条电线,电线都会有电阻,求起始节点和终止节点之间的等效电阻
思路:由基尔霍夫电流定律,每个节点的流入电流与流出电流量是相等的,根据这条信息我们即可列出相关方程。
例如下图,以节点2为例可列方程,I1,I2,I3分别是在单位电势下流经节点(1,2),(2,3),(2,4)之间电线的电流,那么我们设x1,x2,x3,x4分别为节点1,2,3,4的电势,流入节点的电流等于流出节点的电流量,则可得到方程:
I1(x1-x2)+I2(x3-x2)+I3(x4-x2)=0;整理一下:I1x1+(-I1-I2-I3)x2+I2x3+I3x4=0;

那么除了初始节点和终止节点,其余的节点都有上述等式成立,则可得到n-2个方程,初始节点和终止节点的电势,我们可以人为的设置一个值,不如就初始节点电势为1,终止节点电势为0,再根据n-2个方程即可得到中间n-2个节点的电势值。

之后只要求出流经整幅图的电流量I_sum,那么等效电阻=(初始结点电势-终止节点电势)/ I_sum==1 / I_sum。

I_sum可以通过计算初始节点的电流流入量或者终止节点电流流出量得到。

AC代码:

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <vector>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
#define MAX_N 500
#define EPS 1e-8
typedef vector<double> vec;
typedef vector<vec> mat;
double resistor[MAX_N][MAX_N]; // 不考虑其他节点影响时,两个节点间的电阻
int N, M;
vec gauss(const mat&A, const vec&b) {
int n = A.size();//!!!!
mat B(n, vec(n + ));
for (int i = ; i < n; i++)
for (int j = ; j < n; j++)B[i][j] = A[i][j];
for (int i = ; i < n; i++)B[i][n] = b[i];
for (int i = ; i < n; i++) {
int pivot = i;
for (int j = i; j < n; j++) {
if (abs(B[j][i]) > abs(B[pivot][i])) {//!!!
pivot = j;
}
}
swap(B[i], B[pivot]);
if (abs(B[i][i]) < EPS)return vec();//无解
for (int j = i + ; j <= n; j++) {
B[i][j] /= B[i][i];
}
for (int j = ; j < n; j++) {
if (i != j) {
for (int k = i + ; k <= n; k++) {
B[j][k] -= B[j][i] * B[i][k];
}
}
}
}
vec x(n);
for (int i = ; i < n; i++) {
x[i] = B[i][n];
}
return x;
} int main() {
while (scanf("%d%d", &N, &M) != EOF) {
memset(resistor, , sizeof(resistor));
for (int i = ; i < M; i++) {
int from, to;
double R;
scanf("%d%d%lf", &from, &to, &R);
if (R == )continue;
from--, to--;
resistor[from][to] += / R;
resistor[to][from] += / R;
}
for (int i = ; i < N; i++) {
for (int j = ; j < N; j++) {
resistor[i][j] = 1.0 / resistor[i][j];
}
}
mat A(N, vec(N, ));
vec b(N, );
b[] = 1.0;
b[N - ] = 0.0;
A[][] = , A[N - ][N - ] = ;
for (int i = ; i < N - ; i++) {
for (int j = ; j < N; j++) {
if (resistor[i][j] > ) {
double I = 1.0 / resistor[i][j];
A[i][i] -= I;
A[i][j] += I;
}
}
}
vec voltage = gauss(A, b);
double current = ;
for (int i = ; i < N; i++) {
if (resistor[][i] > ) {
current += (voltage[] - voltage[i]) / resistor[][i];
}
}
printf("%.2f\n", 1.0 / current);
}
return ;
}

poj 3532 Resistance的更多相关文章

  1. POJ 3532 Resistance(高斯消元+基尔霍夫定理)

    [题目链接] http://poj.org/problem?id=3532 [题目大意] 给出n个点,一些点之间有电阻相连,求1~n的等效电阻 [题解] 有基尔霍夫定理:任何一个点(除起点和终点)发出 ...

  2. poj3532求生成树中最大权与最小权只差最小的生成树+hoj1598俩个点之间的最大权与最小权只差最小的路经。

    该题是最小生成树问题变通活用,表示自己开始没有想到该算法:先将所有边按权重排序,然后枚举最小边,求最小生成树(一个简单图的最小生成树的最大权是所有生成树中最大权最小的,这个容易理解,所以每次取最小边, ...

  3. poj 2892

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7725   Accepted: 3188 D ...

  4. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  5. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  6. hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

    Tunnel Warfare                                                             Time Limit: 4000/2000 MS ...

  7. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  8. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  9. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

随机推荐

  1. java基础—多态(动态加载)

    一.面向对象最核心的机制——动态绑定,也叫多态

  2. java基础—基础语法1

    一.标识符

  3. TCP/UDP 协议介绍

    TCP/IP五层网络结构模型 物理层:物理层建立在物理通信介质的基础上,作为系统和通信介质的接口,用来实现数据链路实体间透明的比特 (bit) 流传输.只有该层为真实物理通信,其它各层为虚拟通信 数据 ...

  4. vector总结(更新中。。。)

    vector中这两个属性很容易弄混淆. size是当前vector容器真实占用的大小,也就是容器当前拥有多少个容器. capacity是指在发生realloc前能允许的最大元素数,即预分配的内存空间. ...

  5. 如何用VS2017用C++语言写Hello world 程序?

    1,首先,打开VS2017. 2,左上角按文件——新建——项目,或按ctrl+shift+n. 3,按照图片里的选,选完按“确定”. 4,右键“源文件”,再按添加——新建项. 5,剩下的就很简单了,只 ...

  6. W3CPLUS DEMO一些有意思的效果备份

    时间轴轮播图: http://www.w3cplus.com/w3cplusDemo/demos/timeline.html css3各种图标效果: http://www.w3cplus.com/w3 ...

  7. 【php】【趣味代码】对象引用的比较

    <?php $a = new stdClass(); $a->name = 'flint'; $b = $a ; $b->sex = 'man'; saveObject($b); f ...

  8. oracle如何保证读一致性 第二弹

    Oracle之数据库一致性读的原理 在Oracle数据库中,undo主要有三大作用:提供一致性读(Consistent Read).回滚事务(Rollback Transaction)以及实例恢复(I ...

  9. POJ:3020-Antenna Placement(二分图的最小路径覆盖)

    原题传送:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Descri ...

  10. Java装饰者模式(Decorator)

    一.定义 装饰模式的设计理念主要是以对客户端透明的方式动态扩展对象的功能,是继承关系的一个替代(继承会产生大量的子类,而且代码有冗余).装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展.装饰 ...