Pagini recente » Cod sursa (job #3128407) | Cod sursa (job #448393) | Cod sursa (job #3263403)
#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;
}