Pagini recente » Cod sursa (job #1553825) | Cod sursa (job #2451129) | Cod sursa (job #1430106) | Cod sursa (job #1874007) | Cod sursa (job #2337469)
#include <bits/stdc++.h>
using namespace std;
ifstream in("hamilton.in");
ofstream out("hamilton.out");
const int N = 20;
const int X = (1<<18)+5;
const int INF = 1e9;
int dp[X][N],Cost[N][N];
vector<int> v[N];
int main()
{
int n,m;
in >> n >> m;
for (int i = 0; i<n; i++)
for (int j = 0; j<n; j++)
Cost[i][j] = INF;
for (int i = 0; i<(1<<n); i++)
for (int j = 0; j<n; j++)
dp[i][j] = INF;
for (int i = 1; i<=m; i++)
{
int x,y,c;
in >> x >> y >> c;
Cost[x][y] = c;
v[x].push_back(y);
}
dp[1][0] = 0;
for (int i = 1; i<(1<<n); i++)
for (int j = 0; j<n; j++)
if ((1<<j)&i)
for (auto it: v[j])
{
if (i&(1<<it))
dp[i][it] = min(dp[i][it],dp[i^(1<<it)][j]+Cost[j][it]);
}
int ans = INF;
for (int i = 0; i<n; i++)
ans = min(ans,dp[(1<<n)-1][i]+Cost[i][0]);
if (ans == INF)
out << "Nu exista solutie";
else
out << ans;
}