Cod sursa(job #2480603)

Utilizator lucametehauDart Monkey lucametehau Data 25 octombrie 2019 20:34:05
Problema Tunelul groazei Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <fstream>
#include <vector>
#include <cmath>

using namespace std;

ifstream cin ("tunel.in");
ofstream cout ("tunel.out");

const double EPS = 1e-9;

int n, m, x, y, z;

vector <pair <int, int>> g[260];
double dp[260][260];

int main() {
  cin >> n >> m;
  for(; m; m--) {
    cin >> x >> y >> z;
    g[x].push_back(make_pair(y, z));
    g[y].push_back(make_pair(x, z));
    dp[x][x]++;
    dp[y][y]++;
  }
  for(int i = 1; i < n; i++) {
    for(auto &j : g[i]) {
      dp[i][n] += j.second;
      if(j.first != n)
        dp[i][j.first]--;
    }
  }
  int nod = 1;
  for(int i = 1; i < n && nod < n; i++) {
    int mn = nod;
    for(int j = nod + 1; j < n; j++) {
      if(fabs(dp[j][i]) > fabs(dp[mn][i]))
        mn = j;
    }
    if(fabs(dp[mn][i]) < EPS)
      continue;
    for(int j = 1; j <= n; j++)
      swap(dp[nod][j], dp[mn][j]);
    double tmp = dp[nod][i];
    for(int j = i; j <= n; j++)
      dp[nod][j] = 1.0 * dp[nod][j] / tmp;
    for(int j = 1; j < n; j++) {
      if(j != nod) {
        tmp = dp[j][i];
        for(int k = 1; k <= n; k++)
          dp[j][k] -= tmp * dp[nod][k];
      }
    }
    nod++;
  }
  cout << dp[1][n];
  return 0;
}