Pagini recente » Cod sursa (job #2521550) | Cod sursa (job #1464897) | Cod sursa (job #355859) | Cod sursa (job #221255) | Cod sursa (job #1159599)
#include <cassert>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
const int MAX_N = 260;
const double EPSILON = 1.e-5;
vector< pair<int, double> > graph[MAX_N];
double coef[MAX_N][MAX_N];
double times[MAX_N];
inline void swap_line(int n, int x, int y) {
for(int j = 1; j <= n + 1; ++ j) {
swap(coef[x][j], coef[y][j]);
}
}
int main() {
ifstream fin("tunel.in");
ofstream fout("tunel.out");
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; ++ i) {
int a, b; double c;
fin >> a >> b >> c;
graph[a].push_back(make_pair(b, c));
graph[b].push_back(make_pair(a, c));
}
coef[1][1] = 1.0;
for(int i = 2; i <= n; ++ i) {
for(int j = 1; j <= n; ++ j) {
coef[i][j] = 0.0;
}
coef[i][i] = -1.0;
double degree = 1.0 / static_cast<double>(graph[i].size());
for(vector< pair<int, double> > :: iterator it = graph[i].begin(); it != graph[i].end(); ++ it) {
coef[i][it->first] += degree;
coef[i][n + 1] -= degree * it->second;
}
}
for(int i = 1; i <= n; ++ i) {
for(int j = 1; j <= n; ++ j) {
if(j > 1) {
fout << "+ ";
}
fout << fixed << setw(3) << setprecision(2) << coef[i][j] << "*t(" << j << ") ";
}
fout << "= " << coef[i][n + 1] << "\n";
}
fout << "\n";
for(int i = n; i > 1; -- i) {
if(fabs(coef[i][i]) < EPSILON) {
for(int j = 1; j <= n; ++ j) {
if(fabs(coef[j][i]) >= EPSILON) {
swap_line(n, i, j);
break;
}
}
assert(0 == 1);
}
for(int j = 1; j < i; ++ j) {
double multiplier = coef[j][i] / coef[i][i];
fout << "Reducing " << j << " with " << i << ", multiplier = " << multiplier << "\n";
for(int k = 1; k <= n + 1; ++ k) {
coef[j][k] -= coef[i][k] * multiplier;
fout << coef[j][k] << (k == n + 1 ? '\n' : ' ');
}
}
}
for(int i = 1; i <= n; ++ i) {
for(int j = 1; j <= n; ++ j) {
if(j > 1) {
fout << "+ ";
}
fout << fixed << setw(3) << setprecision(2) << coef[i][j] << "*t(" << j << ") ";
}
fout << "= " << coef[i][n + 1] << "\n";
}
for(int i = 1; i <= n; ++ i) {
for(int j = 1; j < i; ++ j) {
coef[i][n + 1] -= coef[i][j] * times[j];
}
times[i] = coef[i][n + 1] / coef[i][i];
//fout << " " << times[i];
}
fout << times[n];
return 0;
}