Pagini recente » Cod sursa (job #1459617) | Cod sursa (job #482811) | Cod sursa (job #1275301) | Cod sursa (job #3241318) | Cod sursa (job #1557697)
#include <cstdio>
#include <vector>
#define maxx (1<<17)+2
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
int N,M,C[maxx][18],cost[18][18];
vector <int> G[18];
inline int min(int x,int y){
if(x < y) return x;
return y;
}
int main(){
freopen("hamilton.in","r",stdin);
freopen("hamilton.out","w",stdout);
scanf("%d %d ",&N,&M);
int x,y,z;
//memset(C,inf,sizeof(C));
for(x = 0;x < N;++x)
for(y = 0;y < N;++y)cost[x][y] = inf;
memset(cost,inf,sizeof(cost));
while(M--){
scanf("%d %d %d ",&x,&y,&z);
G[y].push_back(x);
cost[x][y] = z;
}
C[1][0] = 0;
int S = inf;
for(int conf = 0;conf < (1<<N);++conf){
for(x = 0;x < N;++x)
if(conf & (1<<x)){
for(y = 0;y < G[x].size();++y)
if(conf & (1<<G[x][y]))
C[conf][x] = min(C[conf][x],C[conf ^ (1<<x)][G[x][y]] + cost[G[x][y]][x]);
}
}
for(x = 0;x < G[0].size();++x)
S = min(S,C[(1<<N)-1][G[0][x]] + cost[G[0][x]][0]);
if(S == inf)printf("Nu exista solutie\n");
else printf("%d\n",S);
return 0;
}