Cod sursa(job #1617242)

Utilizator Master011Dragos Martac Master011 Data 27 februarie 2016 12:53:23
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include<cstdio>
using namespace std;

const int nMax = 30000 + 1, mMax = 100024 + 1;
int nodV[mMax * 2], urm[mMax * 2], lst[nMax], dist[nMax], lung[mMax * 2];
bool viz[nMax];
int n, m, x, y, nrE;

void adauga(int a, int b, int d){
    nodV[++nrE] = b;
    lung[nrE] = d;
    urm[nrE] = lst[a];
    lst[a] = nrE;
}

void dfs(int nod){

    viz[nod] = true;

    int pos = lst[nod];
    int vec = nodV[pos];

    while(pos){
        if(!viz[vec]){
            if(vec > nod)
                dist[vec] = dist[nod] + lung[pos];
            else
                dist[vec] = dist[nod] - lung[pos];
            dfs(vec);
        }

        pos = urm[pos];
        vec = nodV[pos];
    }
}

int main (){
    FILE *in = fopen("sate.in","r");
    FILE *out = fopen("sate.out","w");

    fscanf(in,"%d%d%d%d", &n, &m, &x, &y);
    int a, b, d;
    for(int i = 1 ; i <= m ; ++i){
        fscanf(in,"%d%d%d", &a, &b, &d);
        adauga(a, b, d);
        adauga(b, a, d);
    }

    dfs(x);

    fprintf(out,"%d", dist[y]);
    return 0;
}