poj 3532 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
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的更多相关文章
- POJ 3532 Resistance(高斯消元+基尔霍夫定理)
[题目链接] http://poj.org/problem?id=3532 [题目大意] 给出n个点,一些点之间有电阻相连,求1~n的等效电阻 [题解] 有基尔霍夫定理:任何一个点(除起点和终点)发出 ...
- poj3532求生成树中最大权与最小权只差最小的生成树+hoj1598俩个点之间的最大权与最小权只差最小的路经。
该题是最小生成树问题变通活用,表示自己开始没有想到该算法:先将所有边按权重排序,然后枚举最小边,求最小生成树(一个简单图的最小生成树的最大权是所有生成树中最大权最小的,这个容易理解,所以每次取最小边, ...
- poj 2892
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7725 Accepted: 3188 D ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】
Tunnel Warfare Time Limit: 4000/2000 MS ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
随机推荐
- Java基础 匿名内部类 异常 多线程 集合面试题
匿名内部类:没有名字的内部类.就是内部类的简化形式.一般只用一次就可以用这种形式.匿名内部类其实就是一个匿名子类对象.想要定义匿名内部类:需要前提,内部类必须继承一个类或者实现接口. 匿名内部类的格式 ...
- NOIP模拟赛 混合图
[题目描述] Hzwer神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. Hzwer的国家有n个点,m条边,而作为国王,他十分喜欢游览自己的国家.他一般会从任意一个点出发,随便找边走,沿途欣赏 ...
- 洛谷 2023 [AHOI2009]维护序列
洛谷 2023 [AHOI2009]维护序列 洛谷原题传送门 这个题也是一道经典的线段树模版(其实洛谷的模版二改一下输入顺序就能AC),其中包括区间乘法修改.区间加法修改.区间查询三个操作. 线段树的 ...
- vtigercrm安装
vtigercrm是一个用户关系管理系统. 本以为安装只用半个小时就可以完成,结果花了两天时间.. 后来因为不想其他的因素影响,重新装了个纯净的系统.(系统为ubuntu16,安装过程略) 在系统基础 ...
- Altium Designer入门学习笔记3:关于各模块分开布线的理解( 1)
观看"杜洋AD的讲解视频",杜洋着重强调了"模块分开"布线的好处. ---------------------------------------------- ...
- python寻找模块的路径顺序
>>> import sys >>> sys.path ['', '/Library/Frameworks/Python.framework/Versions/3. ...
- 计蒜客 The 2018 ACM-ICPC Chinese Collegiate Programming Contest Rolling The Polygon
include <iostream> #include <cstdio> #include <cstring> #include <string> #i ...
- Linux学习-什么是进程 (process)
触发 任何一个事件时,系统都会将他定义成为一个进程,并且给予这个进程一个 ID ,称为 PID,同时依据启发这个进程的用户与相关属性关系,给予这个 PID 一组有效的权限设定.从此以后,这 个 PID ...
- CDH4 journalnode方式手工安装手册之一
一. 环境部署概况 cdh-master 172.168.10.251 cdh-node1 172.168.10.251 cdh-no ...
- luogu2756 飞行员配对方案问题
匈牙利 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...