Cod sursa(job #938858)

Utilizator ericptsStavarache Petru Eric ericpts Data 14 aprilie 2013 01:32:42
Problema Ciclu hamiltonian de cost minim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <fstream>
#include <vector>
#include <cstring>
#include <iostream>

using namespace std;

#define pb push_back
#define forEach(G) for(typeof(G.begin()) it = G.begin() ; it != G.end() ; ++ it)
#define mp make_pair

ifstream in("hamilton.in");
ofstream out("hamilton.out");
int n,m;
const int maxn = 20;
int best[1<<maxn][maxn];
vector< pair<int,int> > G[maxn];

int main()
{
    in >> n >> m;
    int x,y,c;
    int i,j;
    while(m--){
        in >> x >> y >> c;
        G[x].pb(mp(y,c));
    }
    memset(best,0x7f,sizeof(best));
    best[1][0] = 0;
    for(i=0;i<(1<<n);++i){
        for(j=0;j<n;++j){
            if(i&(1<<j)){
                forEach(G[j]){
                    if(i&(1<<it->first)){
                        best[i][j] = min(best[i][j],best[i^(1<<j)][it->first] + it->second);
                    }
                }
            }
        }
    }
    int show = 1 << 30;--i;
    forEach(G[0]){
        show = min(show,best[i][it->first]+it->second);
    }
    if(show == 1 << 30)
        out << "Nu exista solutie\n";
    else out << show << "\n";
    return 0;
}