Cod sursa(job #1400402)

Utilizator BKmarianmarian BKmarian Data 25 martie 2015 11:41:22
Problema Sate Scor 60
Compilator cpp Status done
Runda Teme Pregatire ACM Unibuc 2013 Marime 1.26 kb
#include <stdio.h>
#include <stdlib.h>
int n, m, x, y, viz[30000];

typedef struct nod
{
    int x;
    long d;
    nod *a;
} *pNod;

pNod v[30001];

void citire()
{
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    int i, a, b;
    long d;
    pNod p;

    scanf("%d %d %d %d",&n,&m,&x,&y);
    for (i = 1; i <= m; i++)
    {
        scanf("%d %d %ld",&a, &b, &d);
        p = new nod;
        p -> x = a;
        p -> d = d;
        p -> a = v[b];
        v[b] = p;
        p = new nod;
        p -> x = b;
        p -> d = d;
        p -> a = v[a];
        v[a] = p;
    }
}

void DF(int nod, long dist)
{
    pNod p;
    if (nod == y)
    {
        printf("%d\n",dist);
        exit(0);
    }
    viz[nod] = 1;

    for (p = v[nod]; p != NULL; p = p -> a)
    {
        if (!viz[p->x])
        if (p -> x < nod)
        {
            dist -= p -> d;
            DF(p -> x, dist);
            dist += p -> d;
        }
        else
        {
            dist += p -> d;
            DF(p -> x, dist);
            dist -= p -> d;
        }
    }
}

int main()
{
    citire();
    if (x > y)
    {
        int aux = x;
        x = y;
        y = aux;
    }
    DF(x,0);
    return 0;
}