Pagini recente » Cod sursa (job #612001) | Cod sursa (job #353981) | Cod sursa (job #1847672) | Cod sursa (job #234279) | Cod sursa (job #2795214)
#include <bits/stdc++.h>
#define fastio std::ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
#define FILES freopen("hamilton.in","r",stdin);\
freopen("hamilton.out","w",stdout);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define add emplace_back
#define MAX 100
#define mod 666013
#define BMAX 524288
using namespace std;
int n,m,x,y,c,dp[BMAX+5][20];
vector <pair<int,int>> mch[MAX+5];
int main()
{
fastio
FILES
cin >> n >> m;
for(int i = 1; i <= m; ++i)
{
cin >> x >> y >> c;
mch[x].add(mp(y,c));
}
int rz = (1 << n) - 1;
for(int i = 0; i <= rz; ++i)
for(int j = 0; j < n; ++j)
dp[i][j] = 1e9;
dp[1][0] = 0;
for(int i = 0; i <= rz; ++i)
{
for(int j = 0; j < n; ++j)
{
if(!(i & (1 << j)))
continue;
for(auto it : mch[j])
{
if(!(i & (1 << it.first)))
continue;
dp[i][it.first] = min(dp[i ^ (1 << it.first)][j] + it.second,dp[i][it.first]);
}
}
}
int cost = 1e9;
for(int j = 1; j < n; ++j)
{
for(auto it : mch[j])
{
if(!it.first)
{
cost = min(cost,dp[rz][j] + it.second);
break;
}
}
}
if(cost == 1e9) cout << "Nu exista solutie";
else
cout << cost;
}