Cod sursa(job #892864)

Utilizator ovidel95Ovidiu ovidel95 Data 26 februarie 2013 11:59:05
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 kb
#include <fstream>
#define nmax 2001
using namespace std;
int a[nmax][nmax],n,m,ls2,l2[16],k,cost8,l8,viz[nmax];
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[l2[q]]==0)
                    aux=1;
            if (aux==0)
            {
                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");
    f>>n>>m>>ls2;
    int i,el1,el2,cc;
    for (i=1;i<=ls2;i++)
        f>>l2[i];
    cost8=0;
    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;
    l8=0;
    back(2,1,0);
    g<<l8;
    f.close();
    g.close();
    return 0;
}