Pagini recente » Cod sursa (job #2739928) | Cod sursa (job #1271906) | Cod sursa (job #2361787) | Cod sursa (job #1896181) | Cod sursa (job #3215841)
#include <bits/stdc++.h>
#define NMAX 18
#define inf 0x3f3f3f3f
using namespace std;
ifstream fin ("hamilton.in");
ofstream fout ("hamilton.out");
int n, m, dp[(1<<NMAX)+2][NMAX], a[NMAX][NMAX];
vector<int> G[NMAX];
void read_data()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[y].push_back(x);
a[x][y] = c;
}
}
int main()
{
read_data();
memset(dp, 0x3f, sizeof(dp));
dp[1][0] = 0;
for (int i = 1; i < (1<<n); i++)
{
for (int j = 1; j <= n; j++)
{
if ((i & (1 << j)) == 0) continue;
for (auto it : G[j])
{
if ((i & (1 << it)) == 0) continue;
dp[i][j] = min(dp[i][j], dp[i^(1<<j)][it] + a[it][j]);
}
}
}
int sol = inf;
for (auto it : G[0])
sol = min(sol, dp[(1<<n)-1][it] + a[it][0]);
if (sol == inf)
fout << "Nu exista solutie";
else fout << sol;
return 0;
}