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

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. 《剑指offer》56 数组中只出现一次的数字

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.   在线练习:https://www.nowcoder.com/practice/e02fdb5 ...

  2. 安装IAR ewarm

    一  安装准备 (ST方案) 1 嵌入式集成开发环境IAR ewarm 5.41 2 J-Link4.20 3 emberznet-4.3.0协议栈安装包 option1:tools - stm32软 ...

  3. CF-1143D. The Beatles

    题意:有间隔为k的n个点在数轴上,下标为 \(1,k+1, 2*k+1,\cdots (n-1)*k+1\) 首尾相接.设起点为s,步长为L,而现在只知道s距离最近的点的距离为a,和(s+L)距离最近 ...

  4. node 文件下载到本地 (支持中文文件名)

    downloadfile:function(req,res,next){ var name= encodeURI(req.query.name); var path= req.query.url; v ...

  5. SpringMVC 项目中引用其他 Module 中的方法

    1. 将要引用的Module 引入项目中 2. 在主Module中添加依赖, 3. 被引用的类必须放在 Module 中/src/下的某个package中,否则引用不到(重要)

  6. Python入门:Python基础笔记

    (C语言:)C语言是相对C++.C#.Java等语言更接近底层,并且一些硬件编程都可以使(只能使用)C语言.另外C语言学起来相对困难,因为涉及到指针,指针也是语言接近底层语言的一个特征.目前编写较大的 ...

  7. vue使用原生js实现滚动页面跟踪导航高亮

    需要使用vue做一个专题页面. 滚动页面指定区域导航高亮. BetterScroll:可能是目前最好用的移动端滚动插件 如何自定义CSS滚动条的样式? 监听滚动页面事件,对比当前页面的位置与元素的位置 ...

  8. usb hub 设备流程图

    在此处负责而来:http://blog.csdn.net/xuelin273/article/details/38646851  下面的转载于:http://blog.csdn.net/qianguo ...

  9. writing a usb driver(在国外的网站上复制下来的)

    Writing a Simple USB Driver   From Issue #120April 2004 Apr 01, 2004  By Greg Kroah-Hartman  in Soft ...

  10. MySQL 的MAC终端一些指令总结

    开启MySQL服务 sudo /usr/local/mysql/support-files/mysql.server start 关闭MySQL服务 udo /usr/local/mysql/supp ...