[HDU3976]Electric resistance(电阻)(信竞&物竞)(高斯消元)
题面
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.
给你一个无向连通图(电路图)
每一条边的边权为这条边的电阻,点1是正极,点n是负极。
求整个电路的等效电阻。
Input
In the first line has one integer T indicates the number of test cases. (T <= 100)
T组数据
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!
n m
u1 v1 w1(R)
......
um vm wm(R)
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 .
Case #idx: answer_x(double)
......
题解
不妨先随便假设电源电压为一个值U,已知量为边的电阻,我们要设未知量来高斯消元解方程。
设每个点的电流有点麻烦,我们设电压。
由于电压指导体两端的电压,不能说某个点的电压,于是我们设U[i]为 以i和n为两端的电路的电压。
显然,U[1] = U,U[n] = 0,U[i]是个未知数。
若电流从u流到v,那么U[u]一定大于U[v]。
由于流入一个点 i 的电流等于流出一个点的电流,
即
由于
那么合并一下,。
这就是方程组中的一个方程,用高斯消元可以把U[...]都求出来,可以证明一定有解。
最后通过点1连接的电阻算出干路电流,再通过欧姆定律算出等效电阻。
这里有一份很不错的高斯消元板子。
CODE
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#define LL long long
#define MAXN 105
#define DB double
#define lowbit(x) ((-x & x))
#define rg register
#define eps (1e-8)
using namespace std;
inline LL read() {
LL f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-') f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0';s = getchar();}
return x * f;
}
LL n;
LL m,i,j,s,o,k;
bool cmp(DB a,DB b) {
if(a-b>eps) return 1;
if(a-b<-eps) return -1;
return 0;
}
bool gauss(int n,DB a[][MAXN]) {
bool flag=1;
for(int i = 1;i <= n;i ++) {
int maxi = i;
for(int j = i+1;j <= n;j ++) {
if(cmp(a[maxi][i],a[j][i]) == -1) {
maxi = j;
}
}
swap(a[maxi],a[i]);
if(cmp(a[i][i],0.0) == 0) {
flag = 0;continue;
}
for(int j = 1;j <= n;j ++) {
if(i==j || cmp(a[j][i],0) == 0) continue;
for(int k = i+1;k <= n+1;k ++) {
a[j][k] -= a[i][k] * a[j][i]/a[i][i];
}
a[j][i] = 0;
}
}
return flag;
}
int gauss_final(int n,DB a[][MAXN],DB *b) {
if(!gauss(n,a)) {
for(int i = 1;i <= n;i ++) {
bool flag = 0;
for(int j = 1;j <= n;j ++) {
if(cmp(a[i][j],0) != 0) {
flag = 1;
}
}
if(!flag && cmp(a[i][n+1],0) != 0) return -1;
}
return 0;
}
for(int i = 1;i <= n;i ++) {
b[i] = a[i][n+1]/a[i][i];
}
return 1;
}
DB a[MAXN][MAXN];
DB U[MAXN];
DB g[MAXN];
queue<int> q;
int main() {
int T = read(),idx=0;
while(T --) {
n = read();m = read();
memset(a,0,sizeof(a));
memset(U,0,sizeof(U));
memset(g,0,sizeof(g));
U[1] = 100.0;U[n] = 0;
while(!q.empty()) q.pop();
for(int i = 1;i <= m;i ++) {
s = read();o = read();k = read();
if(s>o) swap(s,o);
int t = o;
if(s == 1) q.push(o),g[o] = k*1.0;
if(s!=1&&s!=n) {
a[s-1][s-1] += 1.0/(DB)k;
if(t!=1&&t!=n) {
a[s-1][t-1] -= 1.0/(DB)k;
}
else if(t==1) {
a[s-1][n-1] += 100.0/(DB)k;
}
}
if(t!=1&&t!=n) {
a[t-1][t-1] += 1.0/(DB)k;
if(s!=1&&s!=n) {
a[t-1][s-1] -= 1.0/(DB)k;
}
else if(s==1) {
a[t-1][n-1] += 100.0/(DB)k;
}
}
}
gauss_final(n-2,a,U+1);
DB I = 0.0;
while(!q.empty()) {
int v = q.front();
q.pop();
I += (U[1] - U[v]) / g[v];
}
printf("Case #%d: %.2lf\n",++idx,U[1]/I);
}
return 0;
}
[HDU3976]Electric resistance(电阻)(信竞&物竞)(高斯消元)的更多相关文章
- BZOJ 2419: 电阻 [高斯消元 物理]
http://www.lydsy.com/JudgeOnline/problem.php?id=2419 题意: n个点m个电阻构成一张图,求1到n的等效电阻 第一节课看一道题弃疗,于是来做这道物理题 ...
- POJ 3532 Resistance(高斯消元+基尔霍夫定理)
[题目链接] http://poj.org/problem?id=3532 [题目大意] 给出n个点,一些点之间有电阻相连,求1~n的等效电阻 [题解] 有基尔霍夫定理:任何一个点(除起点和终点)发出 ...
- HDU5006 Resistance(高斯消元)
给你一个复杂的网路图,然后告诉你s,t,求s,t的等效电阻.方法是设s的电势为1,t的电势为0.然后对于其它的每个点x,满足的是sigma(ux-uy)/R(x,y)(即对每个与x相连的节点y,电势差 ...
- 【期望DP+高斯消元】BZOJ3270-博物馆
[题目大意] 有m条走廊连接的n间房间,并且满足可以从任何一间房间到任何一间别的房间.两个男孩现在分别处在a,b两个房间,每一分钟有Pi 的概率在这分钟内不去其他地方(即呆在房间不动),有1-Pi 的 ...
- HDU 3976 Electric resistance (高斯消元法)
Electric resistance Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- [Gauss]HDOJ3976 Electric resistance
题意: 一看图就明白了 要求的是1与n端点间的等效电阻 重点在于转化成考虑电流 根据KCL定理:在任一瞬间流出(流入)该节点的所有电流的代数和恒为零 U = IR 可以令1点的电势为零 那么n点的电势 ...
- 友链&&日记
上面友链,下面日记 友人链 最喜欢galgameの加藤聚聚 初三一本&&\(ACG\)姿势比我还丰厚的yx巨巨 更喜欢galgame的shadowice czx ZigZag胖胖 文文 ...
- poj 2096 Collecting Bugs (概率dp 天数期望)
题目链接 题意: 一个人受雇于某公司要找出某个软件的bugs和subcomponents,这个软件一共有n个bugs和s个subcomponents,每次他都能同时随机发现1个bug和1个subcom ...
- NOI 国家集训队论文集
鉴于大家都在找这些神牛的论文.我就转载了这篇论文合集 国家集训队论文分类 组合数学 计数与统计 2001 - 符文杰:<Pólya原理及其应用> 2003 - 许智磊:<浅谈补集转化 ...
随机推荐
- vue组件data函数
vue组件data通常定义为一个函数并return一个对象,对象中定义的就是组件数据,当然定义数据还有props.computed等方式. data如果直接定义为对象data: {message: ' ...
- Spring基础只是—AOP的概念介绍
Spring容器包含两个重要的特性:面向切面编程(AOP)和控制反转(IOC).面向切面编程是面向对象(OOP)的一种补充,在面向对象编程的过程中编程针对的目标是一个个对象,而面向切面编程中编程针对的 ...
- 第六章、PXE高效网络装机、Kickstart无人值守安装
目录 一.部署PXE远程安装服务 1PXE定义 2PXE服务优点 3搭建网络体系前提条件 4PXE实现过程讲解 二.搭建PXE远程安装服务器 三.Kickstart无人值守安装 一.部署PXE远程安装 ...
- 华为云发布桌面IDE-CodeArts
摘要:华为伙伴暨开发者大会2022,发布华为云桌面IDE-CodeArts. 本文分享自华为云社区<华为云发布桌面IDE-CodeArts,让连接更简单.编码更智能>,作者: Huawei ...
- BUUCTF-小明的保险箱
小明的保险箱 16进制打开可以发现存在一个RAR压缩包,压缩包里面应该就是flag文本 使用ARCHPR破解即可
- Vue3.0系列——「vue3.0性能是如何变快的?」
前言 先学习vue2.x,很多2.x内容依然保留: 先学习TypeScript,vue3.0是用TS重写的,想知其然知其所以然必须学习TS. 为什么学习vue3.0? 性能比vue2.x快1.2-2倍 ...
- React与Koa一起打造一个功能丰富的全栈个人博客(业务篇)
前言 豆哥的个人博客又改版了,本版主要技术栈是前台用的React,后台用的Koa.博客改版的初衷是自己可以练练React(公司的项目部分要用React,我也没法啊,再说早晚得学).本文主要介绍博客的业 ...
- Vue 安装 vue的基本使用 vue的初步使用步骤
1. 资源: https://cn.vuejs.org/v2/guide/#%E8%B5%B7%E6%AD%A5 进入官网学习 2. 点击安装,要把vue下载到本地文件的根目录中,不要选择压缩版的,这 ...
- WPF开发随笔收录-带递增递减按钮TextBox
一.前言 今天分享一下如何实现带递增递减按钮的TextBox控件 二.正文 1.之前的博客分享了一篇自定义XamlIcon控件的文章,这次就直接在那个项目的基础上实现今天这个自定义控件 2.首先添加两 ...
- Linux操作系统(4):磁盘分区、挂载
Outline: ① lsblk :查看所有设备挂载情况 ② df -h :查询系统整体磁盘使用情况 ③ du -h /目录 :查询指定目录的磁盘占用情况 ④ mount :查询系统中已经挂载的设备 ...