题意:
一些导线连接一些点,导线的电阻只有0和1.求S和T的等效电阻。
题解:
这题给re跪了,不过思路应该不会错,注意高斯消元的姿势。首先对于0电阻的这导线显然无法直接用高斯消元,先度这些点缩点,0电阻的缩成一个点,接着就可以根绝高斯消元列方程了。555
#include<iostream> #include<math.h> #include<stdio.h> #include<algorithm> #include<string.h> #include<string> #include<vector> #include<queue> #include<map> #include<set> #include<stack> #define B(x) (1<<(x)) using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned ui; const int oo = 0x3f3f3f3f; const ll OO = 0x3f3f3f3f3f3f3f3f; const double eps = 1e-9; #define lson rt<<1 #define rson rt<<1|1 void cmax(int& a, int b){ if (b > a)a = b; } void cmin(int& a, int b){ if (b < a)a = b; } void cmax(ll& a, ll b){ if (b > a)a = b; } void cmin(ll& a, ll b){ if (b < a)a = b; } void cmax(double& a, double b){ if (a - b < eps) a = b; } void cmin(double& a, double b){ if (b - a < eps) a = b; } void add(int& a, int b, int mod){ a = (a + b) % mod; } void add(ll& a, ll b, ll mod){ a = (a + b) % mod; } const ll MOD = 1000000007; const int maxn = 11000; double a[1005][1005]; int fa[maxn], id[maxn]; struct line{ int u, v, c; }edge[maxn << 2]; int cnt; double ans; int find(int x){ return x == fa[x] ? x : fa[x] = find(fa[x]); } void merge(int u, int v){ int x = find(u); int y = find(v); if (x != y) fa[x] = y; } void Gauss(int n, int m){ int r, c, k; for (r = c = 0; r < n && c < m; r++, c++){ for (k = r; k < n; k++) if (fabs(a[k][c]) > eps) break; if (r == n) continue; if (k != r){ for (int j = 0; j <= m; j++) swap(a[k][j], a[r][j]); } for (int j = c + 1; j <= m; j++)a[r][j] /= a[r][c]; a[r][c] = 1.0; for (int i = 0; i < n; i++){ if (i == r || fabs(a[i][c]) < eps) continue; for (int j = c + 1; j <= m; j++) a[i][j] -= a[i][c] * a[r][j]; a[i][c] = 0.0; } } } void InitFa(int n){ for (int i = 0; i <= n; i++) fa[i] = i; } void Init(int n){ for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) a[i][j] = 0.0; } void gao(int n){ cnt = 0; for (int i = 1; i <= n; i++){ if (find(i) == i) id[i] = cnt++; } for (int i = 1; i <= n; i++){ id[i] = id[find(i)]; } } int main(){ //freopen("E:\\read.txt", "r", stdin); int n, m, u, v, c, S, T, cas; scanf("%d", &cas); while (cas--){ scanf("%d %d %d %d", &n, &m, &S, &T); InitFa(n); for (int i = 1; i <= m; i++){ scanf("%d %d %d", &u, &v, &c); edge[i].u = u; edge[i].v = v; edge[i].c = c; if (c == 0) merge(u, v); } gao(n); //缩点后成为一个点,电阻肯定是0 if (find(S) == find(T)){ printf("%.6lf\n", 0); continue; } //如果两个点不连通电阻是无穷大 InitFa(n); for (int i = 1; i <= m; i++){ u = edge[i].u; v = edge[i].v; merge(u, v); } if (find(S) != find(T)){ printf("inf\n"); continue; } Init(n); for (int i = 1; i <= m; i++){ u = id[edge[i].u]; v = id[edge[i].v]; c = edge[i].c; if (u == v) continue; a[u][v] -= 1.0; a[u][u] += 1.0; a[v][u] -= 1.0; a[v][v] += 1.0; } S = id[S]; T = id[T]; a[S][cnt] = 1.0; a[T][cnt] = -1.0; a[cnt - 1][S] = 1; Gauss(cnt, cnt); printf("%.6lf\n", a[S][cnt] / a[S][S] - a[T][cnt] / a[T][T]); } return 0; }