Cod sursa(job #3263403)

Utilizator robert_dumitruDumitru Robert Ionut robert_dumitru Data 14 decembrie 2024 11:11:50
Problema Distante Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <bits/stdc++.h>
using namespace std;


ifstream fin("ciclu.in");
ofstream fout("ciclu.out");

int n, m;
double sol;
int st[1005], viz[1005], a[1005][1005];
vector<pair<int, int>> L[1005];

void Back(int top, int cost)
{

    if (top > 2 && a[st[top - 1]][st[1]] != 0)
        sol = min (sol, 1.0 * (a[st[top - 1]][st[1]] + cost) / (top - 1));
    else
        for (auto w : L[st[top - 1]])
            if (viz[w.first] == 0)
            {
                st[top] = w.first;
                viz[w.first] = 1;
                Back(top + 1, cost + w.second);
                viz[w.first] = 0;
            }
}

int main()
{
    int i, j, c;
    fin >> n >> m;
    while (m--)
    {
        fin >> i >> j >> c;
        L[i].push_back({j, c});
        a[i][j] = c;
    }
    for (i = 1; i <= n; i++)
        L[0].push_back({i, 0});

    sol = 2e9;
    Back(1, 0);
    fout << fixed << setprecision(2) << sol << "\n";
    return 0;
}