Pagini recente » Cod sursa (job #1945196) | Cod sursa (job #324047) | Cod sursa (job #2582104) | Cod sursa (job #2274084) | Cod sursa (job #1758226)
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
#define NMAX 20
#define MMAX 263000
#define INFI 0x3f3f3f3f
vector < int > g[ NMAX ];
int v[ MMAX ][ NMAX ],
cost[ NMAX ][ NMAX ];
int compute( int mask, int nod ){
vector < int > :: iterator it;
if( v[ mask ][ nod ] == -1 ){
v[ mask ][ nod ] = INFI;
for( it = g[ nod ].begin(); it != g[ nod ].end(); ++it ){
if( mask & ( 1 << *it ) ){
if( *it == 0 && mask != ( 1 << nod ) + 1 ) continue ;
v[ mask ][ nod ] = min( v[ mask ][ nod ], compute( mask ^ ( 1 << nod ), *it ) + cost[ *it ][ nod ] );
}
}
}
return v[ mask ][ nod ];
}
int main()
{
freopen("hamilton.in","r",stdin);
freopen("hamilton.out","w",stdout);
int n, m, i, j, x, y, sol;
vector < int > :: iterator it;
scanf("%d%d",&n,&m);
while( m-- ){
scanf("%d%d",&x,&y);
scanf("%d",&cost[ x ][ y ]);
g[ y ].push_back( x );
}
memset( v, -1, sizeof( v ));
for( i = 0; i < n; ++i ) v[ 1 << i ][ i ] = 0;
sol = INFI;
for( it = g[ 0 ].begin(); it != g[ 0 ].end(); ++it )
sol = min( sol, compute( ( 1 << n ) - 1, *it ) + cost[ *it ][ 0 ] );
if( sol != INFI ) printf("%d\n",sol);
else printf("Nu exista solutie\n");
return 0;
}