Pagini recente » Cod sursa (job #2209897) | Cod sursa (job #2089525) | Cod sursa (job #1819511) | Cod sursa (job #2343874) | Cod sursa (job #2170572)
#include <fstream>
using namespace std;
ifstream f("hamilton.in");
ofstream g("hamilton.out");
struct nod
{
int val,cost;
nod *urm;
};
typedef nod *pnod;
#define inf 1000000000
pnod v[20],p;
int n,m,minim=inf,be[20],act,exista[20];
void ad(int x,int y,int c)
{
p=new nod;
p->val=y;
p->cost=c;
p->urm=v[x]->urm;
v[x]->urm=p;
}
void ciclu(int i,int nivel)
{
if(nivel==n)
{
if(exista[i]!=0)
minim=min(minim,act+exista[i]);
}
{be[i]=1;
pnod p=v[i]->urm;
while(p)
{
if(be[p->val]==0)
{
act+=p->cost;
ciclu(p->val,nivel+1);
act-=p->cost;
}
p=p->urm;
}
be[i]=0;}
}
int main()
{
int i,x,y,c;
f>>n>>m;
for(i=0;i<n;i++)
{
v[i]=new nod;
v[i]->urm=0;
}
for(i=1;i<=m;i++)
{
f>>x>>y>>c;
ad(x,y,c);
if(y==0)
exista[x]=c;
}
ciclu(0,1);
if(minim==inf)
g<<"Nu exista solutie";
else
g<<minim;
return 0;
}