UVA 10808 - Rational Resistors(高斯消元+并查集+分数+基尔霍夫定律)

UVA 10808 - Rational Resistors

题意:给定一些结点,有一些电阻,电阻分布在边上,给定一个电路图,每次询问两点,求这两点间的等效电阻

思路:根据基尔霍夫定律,任意一点的电流向量为0,这样就能设每个结点的电势,列出方程,利用高斯消元求解,对于无解的情况,肯定是两点不能连通,这个可以利用并查集判断。

此外这题有个很坑的地方啊,就是高斯消元的姿势不够优美就会爆long long

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long ll;

struct Frac {

    ll a, b;

    Frac() {a = 0; b = 1;}
    Frac(ll a, ll b) {this->a = a; this->b = b; deal();}

    void init() {a = 0; b = 1;}

    ll gcd(ll a, ll b) {
	while (b) {
	    ll tmp = a % b;
	    a = b;
	    b = tmp;
	}
	return a;
    }

    void deal() {
	ll d = gcd(a, b);
	a /= d; b /= d;
	if (b < 0) {
	    a = -a;
	    b = -b;
	}
    }

    Frac operator + (Frac c) {
	Frac ans;
	ans.a = a * c.b + b * c.a;
	ans.b = b * c.b;
	ans.deal();
	return ans;
    }

    Frac operator - (Frac c) {
	Frac ans;
	ans.a = a * c.b - b * c.a;
	ans.b = b * c.b;
	ans.deal();
	return ans;

    }

    Frac operator * (Frac c) {
	Frac ans;
	ans.a = a * c.a;
	ans.b = b * c.b;
	ans.deal();
	return ans;
    }

    Frac operator / (Frac c) {
	Frac ans;
	ans.a = a * c.b;
	ans.b = b * c.a;
	ans.deal();
	return ans;
    }

    void operator += (Frac c) {*this = *this + c;}
    void operator -= (Frac c) {*this = *this - c;}
    void operator *= (Frac c) {*this = *this * c;}
    void operator /= (Frac c) {*this = *this / c;}

    bool operator > (Frac c) {return a * c.b > b * c.a;}
    bool operator == (Frac c) { return a * c.b == b * c.a;}
    bool operator < (Frac c) {return !(*this < c && *this == c);}
    bool operator >= (Frac c) {return !(*this < c);}
    bool operator <= (Frac c) {return !(*this > c);}
    bool operator != (Frac c) {return !(*this == c);}
    bool operator != (ll c) {return *this != Frac(c, 1);}

    void operator = (ll c) {this->a = c; this->b = 1;}
};

const int N = 20;

int t, n, m, parent[N], node[N];
Frac g[N][N], A[N][N];

int find(int x) {
    return x == parent[x] ? x : parent[x] = find(parent[x]);
}

Frac gauss(int n, int u, int v) {
    for (int i = 0; i < n; i++) {
	int r;
	for (r = i; r < n; r++)
	    if (A[r][i] != 0)
		break;
	if (r == n) continue;
	for (int j = 0; j <= n; j++) swap(A[i][j], A[r][j]);
	for (int j = n; j > i; j--) A[i][j] /= A[i][i];
	A[i][i] = 1;
	for (int j = 0; j < n; j++) {
	    if (i == j) continue;
	    if (A[j][i] != 0) {
		for (int k = n; k > i; k--)
		    A[j][k] -= A[j][i] * A[i][k];
		A[j][i] = 0;
	    }
	}
    }
    return A[u][n] / A[u][u] - A[v][n] / A[v][v];
}

Frac solve(int u, int v) {
    int tu, tv, tn = 0;
    for (int i = 0; i < n; i++) {
	if (i == u) tu = tn;
	if (i == v) tv = tn;
	if (find(u) == find(i)) node[tn++] = i;
    }
    tn++;
    for (int i = 0; i < tn; i++)
	for (int j = 0; j <= tn; j++)
	    A[i][j].init();
    for (int i = 0; i < tn - 1; i++) {
	for (int j = 0; j < tn - 1; j++) {
	    if (i == j) continue;
	    int u = node[i], v = node[j];
	    A[i][i] += g[u][v];
	    A[i][j] -= g[u][v];
	}
    }
    A[tu][tn] = 1;
    A[tv][tn] = -1;
    A[tn - 1][0] = 1;
    return gauss(tn, tu, tv);
}

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
	    parent[i] = i;
	    for (int j = 0; j < n; j++)
		g[i][j].init();
	}
	int u, v;
	ll r;
	while (m--) {
	    scanf("%d%d%lld", &u, &v, &r);
	    if (u == v) continue;
	    g[u][v] += Frac(1, r);
	    g[v][u] += Frac(1, r);
	    int pu = find(u);
	    int pv = find(v);
	    if (pu != pv)
		parent[pu] = pv;
	}
	scanf("%d", &m);
	printf("Case #%d:\n", ++cas);
	while (m--) {
	    scanf("%d%d", &u, &v);
	    int pu = find(u);
	    int pv = find(v);
	    printf("Resistance between %d and %d is ", u, v);
	    if (pu != pv) printf("1/0\n");
	    else {
		Frac ans = solve(u, v);
		printf("%lld/%lld\n", ans.a, ans.b);
	    }
	}
	printf("\n");
    }
    return 0;
}

UVA 10808 - Rational Resistors(高斯消元+并查集+分数+基尔霍夫定律)

时间: 2025-01-02 15:24:18

UVA 10808 - Rational Resistors(高斯消元+并查集+分数+基尔霍夫定律)的相关文章

uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)

题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,任何一点的电流向量为0.就是说有多少电流流入该节点,就有多少电流流出. 对于每次询问的两点间等效电阻,先判断说两点是否联通,不连通的话绝逼是1/0(无穷大).联通的话,将同一个联通分量上的节点都扣出来,假设电势作为变元,然后根据基尔霍夫定律列出方程,因为对于每个节点的电流向量为0,所以每个节点都有一个方程,所有与该节点

uva 1564 - Widget Factory(高斯消元+逆元)

题目链接:uva 1564 - Widget Factory 题目大意:n种零件,m次工作日程,零件序号从1到n,给出m次工作日程的信息,x,s,e,表示生产了x个零件,从星期s开始到星期e(有可能是多个星期),然后给出生产的x个零件的序号.求每个零件被生产需要多少天(保证在3到10天) 解题思路:因为不能确定每个工作日程具体生产了几天,所以对应列出的方程均为线性模方程(模7),所以在高斯消元的过程中遇到除法要转换成乘上逆元. #include <cstdio> #include <cs

UVA 1358 - Generator(dp+高斯消元+KMP)

UVA 1358 - Generator 题目链接 题意:有m种字符(从'A'开始往后数的大写字母),现在有一个字符串,长度不超过12,现在每次随机生成一个字母,要求能产生该字符串的期望长度 思路:dp[i]表示产生长度i的期望长度,那么每次产生一个字符,对应m种转移,每种转移的概率为1/m,转移后的长度可以利用KMP的next数组去快速获得,然后由于转移可能形成环的情况,所以无法直接DP,利用高斯消元去解方程组 代码: #include <cstdio> #include <cstri

UVA 1397 - The Teacher&#39;s Side of Math(高斯消元)

UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n,求原方程组 思路:由于m*n最多20,所有最高项只有20,然后可以把每个此项拆分,之后得到n种不同无理数,每一项为0,就可以设系数为变元,构造方程进行高斯消元 一开始用longlong爆了,换成分数写法也爆了,又不想改高精度,最后是机智的用了double型过的,不过用double精度问题,所以高斯消元的姿势要正确,并且最后输出要注意-0的情况 代码: #include <c

uva 1560 - Extended Lights Out(枚举 | 高斯消元)

题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5?6的矩阵,每个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,如果按了这个位置的开关,将会导致周围包括自己位置的灯状态变换,求一个按开关位置,保证所有灯都灭掉. 解题思路: 枚举,枚举第一行的状态,然后递推出后面四行的状态. 高斯消元,对于每个位置对定变量,这样列出30个方程求解. C++ 枚举 #include <cstdio> #include <cstring> #include &

UVA 1560 - Extended Lights Out(高斯消元)

UVA 1560 - Extended Lights Out 题目链接 题意:给定一个矩阵,1代表开着灯,0代表关灯,没按一个开关,周围4个位置都会变化,问一个按的方法使得所有灯都变暗 思路:两种做法: 1.枚举递推 这个比较简单,就枚举第一行,然后递推过去,每次如果上一行是亮灯,则下一行开关必须按下去 2.高斯消元, 这个做法比较屌一些,每个位置对应上下左右中5个位置可以列出一个异或表达式,然后30个位置对应30个异或表达式,利用高斯消元法就能求出每个位置的解了 代码: 高斯消元法: #inc

UVa 10828 Back to Kernighan-Ritchie 高斯消元+概率DP

题目来源:UVa 10828 Back to Kernighan-Ritchie 题意:从1开始 每次等概率从一个点到和他相邻的点 有向 走到不能走停止 求停止时每个点的期望 思路:写出方程消元 方程有唯一解 多解 无解的情况 有环 一直再环里无法停止算无穷大 从1不能到的点期望为0 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <

UVA 1397 - The Teacher&amp;#39;s Side of Math(高斯消元)

UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个此项拆分.之后得到n种不同无理数,每一项为0.就能够设系数为变元.构造方程进行高斯消元 一開始用longlong爆了.换成分数写法也爆了,又不想改高精度.最后是机智的用了double型过的,只是用double精度问题,所以高斯消元的姿势要正确,而且最后输出要注意-0的情况 代码: #include

UVA 1564 - Widget Factory(高斯消元)

UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间,每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每个零件的制作时间,还要判断一下多解和无解的情况 思路:对于每段时间列出一个方程,这样一共列出m个方程解n个变元,利用高斯消元去求解,注意每个方程都是MOD 7的,所以在高斯消元过程中遇到除法要求该数字%7的逆元去进行运算 代码: #include <cstdio> #include <cstring> #i