Pagini recente » Cod sursa (job #2237125) | Cod sursa (job #901591) | Cod sursa (job #84033) | Cod sursa (job #2520178) | Cod sursa (job #1159557)
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
const int MAX_N = 260;
vector< pair<int, double> > graph[MAX_N];
double coef[MAX_N][MAX_N];
double times[MAX_N];
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(1) << coef[i][j] << "*t(" << j << ") ";
}
fout << "= " << coef[i][n + 1] << "\n";
}
fout << "\n";**/
for(int i = n; i > 1; -- i) {
for(int j = 1; j < i; ++ j) {
double multiplier = coef[j][i] / coef[i][i];
for(int k = 1; k <= n + 1; ++ k) {
coef[j][k] -= coef[i][k] * multiplier;
}
}
}
/**for(int i = 1; i <= n; ++ i) {
for(int j = 1; j <= n; ++ j) {
if(j > 1) {
fout << "+ ";
}
fout << fixed << setw(3) << setprecision(1) << 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;
}