Cod sursa(job #2623567)

Utilizator pregoliStana Andrei pregoli Data 3 iunie 2020 13:56:15
Problema Ciclu hamiltonian de cost minim Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.13 kb
#include <bits/stdc++.h>
#define newline '\n'
using namespace std;
ifstream fin("hamilton.in");
ofstream fout("hamilton.out");
///***********************
const int NMAX = 20, OO = 1e8;
int cost[NMAX][NMAX], perm[NMAX], n, m;
bool used[NMAX];
int ans = OO;

void solve()
{
    int pAns = cost[perm[n]][perm[1]];
    for (int i = 1; i < n; i++)
        pAns += cost[perm[i]][perm[i + 1]];
    ans = min(ans, pAns);
}

void backTr(int cn)
{
    if (cn > n)
    {
        solve();
        return;
    }

    for (int i = 0; i < n; i++)
        if (!used[i])
        {
            used[i] = true;
            perm[cn] = i;
            backTr(cn + 1);
            used[i] = false;
        }//*/
}

int main()
{
    fin >> n >> m;
    for (int i = 0; i < NMAX; i++)
        for (int j = 0; j < NMAX; j++)
            cost[i][j] = OO;

    for (int i = 1; i <= m; i++)
    {
        int x, y;
        fin >> x >> y;
        fin >> cost[x][y];
    }

    backTr(1);//*/
    if (ans == OO)
        fout << "Nu exista solutie";
    else
        fout << ans;
    fout << newline;
    fout.close();
    return 0;
}