Pagini recente » Cod sursa (job #2251212) | Cod sursa (job #101877) | Cod sursa (job #2848317) | Cod sursa (job #39390) | Cod sursa (job #2499116)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream in("hamilton.in");
ofstream out("hamilton.out");
int n,m,a,b,c,dp[(1<<19)][19],y[20];
vector<pair<int,int> > v[20];
int main()
{
in>>n>>m;
for(int i=1;i<=m;i++)
{
in>>a>>b>>c;
v[a].push_back(make_pair(b,c));
if(b==0)y[a]=1;
}
int inf=0x3f3f3f3f;
for(int i=1;i<=(1<<n)-1;i++)
{
for(int j=0;j<n;j++)
{
dp[i][j]=inf;
}
}
dp[1][0]=0;
for(int i=1;i<((1<<n)-1);i++)
{
for(int j=0;j<n;j++)
{
if(i&(1<<j))
{
for(auto t:v[j])
{
if(!(i&(1<<t.first)))
{
dp[i+(1<<t.first)][t.first]=min(dp[i+(1<<t.first)][t.first],dp[i][j]+t.second);
}
}
}
}
}
int rez=inf;
for(int i=0;i<n;i++)
{
for(auto j:v[i])
{
if(j.first==0)
{
rez=min(rez,dp[(1<<n)-1][i]+j.second);
}
}
}
if(rez<inf)
out<<rez;
out<<"Nu exista solutie";
return 0;
}