Cod sursa(job #1708255)

Utilizator Cham3leonMitocaru Andreea Cham3leon Data 26 mai 2016 20:04:49
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <stdio.h>
#include <stdlib.h>
#include <fstream>

using namespace std;

ifstream f("sate.in");
ofstream g("sate.out");

int N,M,X,Y;
int i,j,D;
int ap[30001],k;

struct Sat
{   int vec,cost;
    Sat *next;
};

Sat *aux;
Sat *v[30001];

void BK(int r, int D)
{
    Sat *p;
    if(r==Y)
    {
        g<<D<<endl;
        return;
    }
    ap[r]= 1;
    p=v[r];
    while(p)
        {if(!ap[p->vec])
            BK(p->vec, p->cost+D);
         p=p->next;}
}
int main()
{
    f>>N>>M>>X>>Y;

    for(k=1;k<=M;k++)
    {
        f>>i>>j>>D;
        if(i>j)
            swap(i, j);

        aux=new Sat;
        aux->cost=-D;
        aux->vec=i;
        aux->next=v[j];
        v[j]=aux;

        aux=new Sat;
        aux->cost=D;
        aux->vec=j;
        aux->next=v[i];
        v[i]=aux;
    }
    BK(X,0);
    f.close();
    g.close();
    return 0;
}