Pagini recente » Cod sursa (job #2908235) | Cod sursa (job #3123748) | Cod sursa (job #3001133) | Cod sursa (job #452312) | Cod sursa (job #1786143)
#include <fstream>
#include <vector>
const int NMAX = 18;
const int INF = 100000000;
using namespace std;
ifstream fin ( "hamilton.in" );
ofstream fout ( "hamilton.out" );
int c[NMAX+5][NMAX+5];
int dp[(1<<NMAX)+5][NMAX+5];
vector <int> a[NMAX+5];
int n;
inline void init () {
for ( int i = 0 ; i < n ; ++i )
for ( int j = 0 ; j < n ; ++j )
c[i][j] = INF;
}
int main() {
int m, my_min, i, j, x, y, k;
fin >> n >> m;
init();
for ( i = 1 ; i <= m ; ++i ) {
fin >> x >> y;
fin >> c[x][y];
a[y].push_back ( x );
}
for ( i = 0 ; i < ( 1 << n ) ; ++ i )
for ( j = 0 ; j < n ; ++ j )
dp[i][j] = INF;
dp[1][0] = 0;
for ( i = 0 ; i < ( 1 << n ) ; ++ i )
for ( j = 0 ; j < n ; ++ j )
if ( i & ( 1 << j ) )
for ( size_t k = 0 ; k < a[j].size() ; ++ k )
if ( i & ( 1 << a[j][k] ) )
dp[i][j] = min ( dp[i][j], dp[i^(1<<j)][a[j][k]] + c[a[j][k]][j] );
my_min = INF;
for ( size_t i = 0 ; i < a[0].size() ; ++ i )
my_min = min ( my_min, dp[(1<<n)-1][a[0][i]] + c[a[0][i]][0] );
if ( my_min == INF )
fout << "Nu exista solutie";
else
fout << my_min;
return 0;
}