Pagini recente » Cod sursa (job #477303) | Cod sursa (job #472361) | Cod sursa (job #1347319) | Cod sursa (job #2469689) | Cod sursa (job #2869309)
#include <bits/stdc++.h>
#define FILES freopen("hamilton.in","r",stdin);\
freopen("hamilton.out","w",stdout);
#define MAX 18
#define fastio ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
using namespace std;
int n,m,a,b,c;
vector<pair<int,int>> v[MAX+5];
int dp[262160][19],cost[19][19];
signed main()
{
fastio
FILES
cin >> n >> m;
for(int i = 1;i <= m; ++i)
{
cin >> a >> b >> c;
a++,b++;
v[a].push_back({b,c});
}
int mask = (1 << n) - 1;
for(int i = 1;i <= mask; ++i)
for(int j = 1;j <= n; ++j) dp[i][j] = 1e8;
for(int i = 1;i <= mask; ++i)
{
int r = __builtin_popcount(i);
if(r == 1)
{
int g = log2(i) + 1;
dp[i][g] = 0;
continue;
}
for(int j = 1;j <= i; j *= 2)
{
if(i & j)
{
int u = log2(j) + 1;
for(auto k : v[u])
{
if((i & (1 << (k.first - 1))) && k.first != u)
{
int z = i ^ (1 << (k.first - 1));
dp[i][k.first] = min(dp[i][k.first],dp[z][u] + k.second);
}
}
}
}
}
int ans = 1e9;
for(int i = 1;i <= n; ++i)
{
for(auto k : v[i]){
if(k.first == 1){
ans = min(ans,dp[mask][i]+k.second);
}
}
}
if(ans >= 1e8) cout << "Nu exista solutie";
else cout << ans;
}