Pagini recente » Cod sursa (job #414451) | Cod sursa (job #2305135) | Cod sursa (job #504752) | Cod sursa (job #386697) | Cod sursa (job #891977)
Cod sursa(job #891977)
#include <fstream>
#define nmax 2001
using namespace std;
int a[nmax][nmax],n,m,l8,viz[nmax],cost8,lk[16],ls2;
void back(int k, int nc, int cost)
{
int x;
if (nc==n)
{ int q,aux=0;
for (q=1;q<=ls2;q++)
if (viz[lk[q]]==0)
aux=1;
if (aux==0)
{
for (q=1;q<=k-1;q++)
cost8=cost;
l8=k-1;
}
}
else
{
for (x=1;x<=n;x++)
if (!viz[x]&&a[nc][x]&&cost+a[nc][x]<cost8)
{
viz[x]=1;
back(k+1,x,cost+a[nc][x]);
viz[x]=0;
}
}
}
int main()
{ ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
int el1,el2,cc,i;
f>>n>>m>>ls2;
for (i=1;i<=ls2;i++)
f>>lk[i];
for (i=1;i<=m;i++)
{
f>>el1>>el2>>cc;
a[el1][el2]=a[el2][el1]=cc;
cost8+=cc;
}
for (i=1;i<=n;i++)
viz[i]=0;
viz[1]=1;
l8=0;
back(2,1,0);
g<<l8;
f.close();
g.close();
return 0;
}