Cod sursa(job #3228939)

Utilizator sebi81georgescuGeorgescu Sebastian sebi81georgescu Data 12 mai 2024 14:20:44
Problema Traseu Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.15 kb
#include <fstream.h>
ifstream f("traseu.in");
ofstream g("traseu.out");
struct nod
{
    nod *next;
    int n1,n2,n3;
};
int n,max=0,vmax,tot,cost;
nod *p,*u;
void add(int a,int b,int c2)
{
    if (!p)
    {
        p=new nod;
        p->n1=a;
        p->n2=b;
        p->n3=c2;
        p->next=0;
        u=p;
    }
    else
    {
        nod *c=new nod;
        c->n1=a;
        c->n2=b;
        c->n3=c2;
        c->next=0;
        u->next=c;
        u=c;
    }
}
int drum(int x, int y)
{
    nod *c=p;
    while (c)
    {
        if (c->n1==x&&c->n2==y) {cost=c->n3;return 1;}
        c=c->next;
    }
    return 0;
}
void ciclu(int vf,int j, int s[61])
{
    int i;
    if (vf==vmax&&j>1) {tot+=s[j-1];}
    else
        for (i=1;i<=n;i++)
            if (drum(vf,i))
            {
                s[j]=s[j-1]+cost;
                ciclu(i,j+1,s);
            }
}
int main()
{
    int m,a,b,i,c,s[61]={0};
    f>>n>>m;
    for (i=1;i<=m;i++)
    {
        f>>a>>b>>c;
        add(a,b,c);
        s[a]++;
        s[b]++;
    }
    for (i=1;i<=n;i++)
        if (s[i]>max) {max=s[i];s[i]=0;vmax=i;}
    ciclu(vmax,1,s);
    g<<tot;
    f.close();
    g.close();
    return 0;
}