Pagini recente » Cod sursa (job #1009236) | Cod sursa (job #253884) | Cod sursa (job #808577) | Cod sursa (job #1516448) | Cod sursa (job #938858)
Cod sursa(job #938858)
#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;
}