uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)
题目链接:uva 10808 - Rational Resistors
题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻。
解题思路:基尔霍夫定律,不论什么一点的电流向量为0。就是说有多少电流流入该节点。就有多少电流流出。
对于每次询问的两点间等效电阻。先推断说两点是否联通。不连通的话绝逼是1/0(无穷大)。联通的话,将同一个联通分量上的节点都扣出来,如果电势作为变元,然后依据基尔霍夫定律列出方程,由于对于每一个节点的电流向量为0。所以每一个节点都有一个方程,全部与该节点直接连接的都会有电流流入。而且最后总和为0,(除了a,b两点,一个为1,一个为-1)。用高斯消元处理,可是这样列出的方程组不能准确求出节点的电势,仅仅能求出各个节点之间电势的关系。所以我们将a点的电势置为0,那么用求出的b点电势减去0就是两点间的电压,又由于电流设为1,所以等效电阻就是电压除以电流。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long type;
struct Fraction {
type member; // 分子;
type denominator; // 分母;
Fraction (type member = 0, type denominator = 1);
void operator = (type x) { this->set(x, 1); }
Fraction operator * (const Fraction& u);
Fraction operator / (const Fraction& u);
Fraction operator + (const Fraction& u);
Fraction operator - (const Fraction& u);
Fraction operator *= (const Fraction& u) { return *this = *this * u; }
Fraction operator /= (const Fraction& u) { return *this = *this / u; }
Fraction operator += (const Fraction& u) { return *this = *this + u; }
Fraction operator -= (const Fraction& u) { return *this = *this - u; }
void set(type member, type denominator);
};
inline type gcd (type a, type b) {
return b == 0 ?
(a > 0 ? a : -a) : gcd(b, a % b);
}
inline type lcm (type a, type b) {
return a / gcd(a, b) * b;
}
/*Code*/
/////////////////////////////////////////////////////
const int maxn = 105;
typedef long long ll;
typedef Fraction Mat[maxn][maxn];
int N, M, f[maxn];
Mat G, A;
bool cmp (Fraction& a, Fraction& b) {
return a.member * b.denominator < b.member * a.denominator;
}
inline int getfar (int x) {
return x == f[x] ? x : f[x] = getfar(f[x]);
}
inline void link (int u, int v) {
int p = getfar(u);
int q = getfar(v);
f[p] = q;
}
void init () {
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) {
f[i] = i;
for (int j = 0; j < N; j++)
G[i][j] = 0;
}
int u, v;
ll R;
for (int i = 0; i < M; i++) {
scanf("%d%d%lld", &u, &v, &R);
if (u == v)
continue;
link(u, v);
G[u][v] += Fraction(1, R);
G[v][u] += Fraction(1, R);
}
}
Fraction gauss_elimin (int u, int v, int n) {
/*
printf("\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j <= n; j++)
printf("%lld/%lld ", A[i][j].member, A[i][j].denominator);
printf("\n");
}
*/
for (int i = 0; i < n; i++) {
int r;
for (int j = i; j < n; j++)
if (A[j][i].member) {
r = j;
break;
}
if (r != i) {
for (int j = 0; j <= n; j++)
swap(A[i][j], A[r][j]);
}
if (A[i][i].member == 0)
continue;
for (int j = i + 1; j < n; j++) {
Fraction t = A[j][i] / A[i][i];
for (int k = 0; k <= n; k++)
A[j][k] -= A[i][k] * t;
}
}
for (int i = n-1; i >= 0; i--) {
for (int j = i+1; j < n; j++) {
if (A[j][j].member)
A[i][n] -= A[i][j] * A[j][n] / A[j][j];
}
}
/*
Fraction U = A[u][n] / A[u][u];
printf("%lld/%lld!\n", A[u][n].member, A[u][n].denominator);
printf("%lld/%lld!\n", A[u][u].member, A[u][u].denominator);
printf("%lld/%lld\n", U.member, U.denominator);
Fraction V = A[v][n] / A[v][v];
printf("%lld/%lld\n", V.member, V.denominator);
*/
return A[u][n] / A[u][u] - A[v][n] / A[v][v];
}
Fraction solve (int u, int v) {
int n = 0, hash[maxn];
int hu, hv;
for (int i = 0; i < N; i++) {
if (i == u)
hu = u;
if (i == v)
hv = v;
if (getfar(i) == getfar(u))
hash[n++] = i;
}
n++;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++)
A[i][j] = 0;
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (i == j)
continue;
int p = hash[i];
int q = hash[j];
A[i][i] += G[p][q];
A[i][j] -= G[p][q];
}
}
A[hu][n] = 1;
A[hv][n] = -1;
A[n-1][0] = 1;
return gauss_elimin (hu, hv, n);
}
int main () {
int cas;
scanf("%d", &cas);
for (int kcas = 1; kcas <= cas; kcas++) {
init();
int Q, u, v;
scanf("%d", &Q);
printf("Case #%d:\n", kcas);
for (int i = 0; i < Q; i++) {
scanf("%d%d", &u, &v);
printf("Resistance between %d and %d is ", u, v);
if (getfar(u) == getfar(v)) {
Fraction ans = solve(u, v);
printf("%lld/%lld\n", ans.member, ans.denominator);
} else
printf("1/0\n");
}
printf("\n");
}
return 0;
}
/////////////////////////////////////////////////////
Fraction::Fraction (type member, type denominator) {
this->set(member, denominator);
}
Fraction Fraction::operator * (const Fraction& u) {
type tmp_p = gcd(member, u.denominator);
type tmp_q = gcd(u.member, denominator);
return Fraction( (member / tmp_p) * (u.member / tmp_q), (denominator / tmp_q) * (u.denominator / tmp_p) );
}
Fraction Fraction::operator / (const Fraction& u) {
type tmp_p = gcd(member, u.member);
type tmp_q = gcd(denominator, u.denominator);
return Fraction( (member / tmp_p) * (u.denominator / tmp_q), (denominator / tmp_q) * (u.member / tmp_p));
}
Fraction Fraction::operator + (const Fraction& u) {
type tmp_l = lcm (denominator, u.denominator);
return Fraction(tmp_l / denominator * member + tmp_l / u.denominator * u.member, tmp_l);
}
Fraction Fraction::operator - (const Fraction& u) {
type tmp_l = lcm (denominator, u.denominator);
return Fraction(tmp_l / denominator * member - tmp_l / u.denominator * u.member, tmp_l);
}
void Fraction::set (type member, type denominator) {
if (denominator == 0) {
denominator = 1;
member = 0;
}
if (denominator < 0) {
denominator = -denominator;
member = -member;
}
type tmp_d = gcd(member, denominator);
this->member = member / tmp_d;
this->denominator = denominator / tmp_d;
}
uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)的更多相关文章
- UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)
UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...
- uva 1560 - Extended Lights Out(枚举 | 高斯消元)
题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5∗6的矩阵,每一个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,假设按了这个位置的开关,将会导致周围包含自 ...
- UVA 12849 Mother’s Jam Puzzle( 高斯消元 )
题目: http://uva.onlinejudge.org/external/128/12849.pdf #include <bits/stdc++.h> using namespace ...
- UVA 11542 - Square(高斯消元)
UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...
- UVA 1397 - The Teacher's Side of Math(高斯消元)
UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个 ...
- UVA 1564 - Widget Factory(高斯消元)
UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间.每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每一个零件的制作时间.还 ...
- UVA 10828 Back to Kernighan-Ritchie(高斯消元)
高斯消元求概率 对于非起点,期望x[i] = ∑x[j] / deg[j] #include<cstdio> #include<iostream> #include<cs ...
- UVa 11542 Square (高斯消元)
题意:给定 n 个数,从中选出一个,或者是多个,使得选出的整数的乘积是完全平方数,求一共有多少种选法,整数的素因子不大于 500. 析:从题目素因子不超过 500,就知道要把每个数进行分解.因为结果要 ...
- UVa 10828 Back to Kernighan-Ritchie (数学期望 + 高斯消元)
题意:给定一个 n 个结点的有向图,然后从 1 结点出发,从每个结点向每个后继结点的概率是相同的,当走到一个没有后继结点后,那么程序终止,然后问你经过每个结点的期望是次数是多少. 析:假设 i 结点的 ...
随机推荐
- [EF]使用EF简单增删改查
目录 认识EF 添加数据 删除数据 修改数据 查询数据 总结 认识EF ADO.NET Entity Framework 是微软以ADO.NET为基础所发展出来的对象关系对伊(O/R Mapping) ...
- GNU ARM 汇编指令
第一部分 Linux下ARM汇编语法尽管在Linux下使用C或C++编写程序很方便,但汇编源程序用于系统最基本的初始化,如初始化堆栈指针.设置页表.操作 ARM的协处理器等.初始化完成后就可以跳转到C ...
- Thunderbird for Ubuntu
转自:http://www.cnblogs.com/slave_wc/archive/2011/05/02/2034529.html 装好ubuntu 的一般基本配置见本博客另一篇文章: Ubun ...
- 网页中输出漂亮格式的Php数组神器
写网页的时候经常需要在页面中打印数组,但格式特别难看,看看一个html神器吧<pre>标签,能非常标准的显示数组格式 使用的时候只需要这样打印你的数组就OK了,太好用了,神器! 只需要两句 ...
- Xcode no visible @interface for XXX declares
出现如上面的错误, 是因为没有找到这个方法, 要自己写一个这样的方法 , 如果这是个类目的方法的话, 需要在Target->Linking->Other Linker Flags中添加- ...
- 初始加载时edittext不自动获取焦点的方法
在做一个搜索功能时,刚进入界面,edittext直接自动获取焦点,这显然不是我想要的. 那么问题来了:如何关闭EditText自动获取焦点的? 尝试了好几个方法,在Java代码添加focus控制,在x ...
- 剖析ASP.NET Core(Part 3)- UseMvc(译)
原文:https://www.stevejgordon.co.uk/asp-net-core-anatomy-part-3-addmvc 发布于:2017年4月环境:ASP.NET Core 1.1 ...
- Python程序员技能表—446家知名企业的Py招聘信息(转载)
Python程序员技能表—446家知名企业的Py招聘信息 转载: python 正在学习python或者想学习python的可以加群:330637182 正在学习python或者想学习python的可 ...
- 如何使用awk的比较操作符
对于使用 awk 命令的用户来说,处理一行文本中的数字或者字符串时,使用比较运算符来过滤文本和字符串是十分方便的.下面的部分我们介绍"awk"的比较运算符. awk 中的比较运算符 ...
- 亲測,Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerException"
Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerException&q ...