http://www.lydsy.com/JudgeOnline/problem.php?id=2419
∑Ui?UjRi,j=0∑Ui?UjRi,j=0
∑U1?UjR1,j=1∑U1?UjR1,j=1
∑Un?UjRi,n=?1∑Un?UjRi,n=?1
Un=0
这就是方程了。。。但是代码有点不理解
#include<bits/stdc++.h> using namespace std; const int N = 510; double a[N][N], g[N][N]; int n, m; void build() { for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j) a[i][i] += g[i][j], a[i][j] -= g[i][j]; a[1][n + 1] = 1.0; a[n][n + 1] = -1.0; a[n][n] += 1.0; } void gauss_jordan() { for(int now = 1; now <= n; ++now) { int x = now; for(int i = now + 1; i <= n; ++i) if(fabs(a[i][now]) > fabs(a[x][now])) x = i; for(int i = 1; i <= n + 1; ++i) swap(a[now][i], a[x][i]); double t = a[now][now]; for(int i = now; i <= n + 1; ++i) a[now][i] /= t; for(int i = 1; i <= n; ++i) if(i != now) { double t = a[i][now]; for(int j = now; j <= n + 1; ++j) a[i][j] -= t * a[now][j]; } } } int main() { while(scanf("%d%d", &n, &m) != EOF) { memset(a, 0, sizeof(a)); memset(g, 0, sizeof(g)); for(int i = 1; i <= m; ++i) { int u, v; double r; scanf("%d%d%lf", &u, &v, &r); if(u == v) continue; g[u][v] += 1.0 / r; g[v][u] += 1.0 / r; } build(); gauss_jordan(); printf("%.2f\n", a[1][n + 1]); } return 0; }
时间: 2024-10-13 21:16:51