Cod sursa(job #1400428)

Utilizator MpowerGarstea Ion Mpower Data 25 martie 2015 11:56:52
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <cstdio>

#define FIN "sate.in"
#define FOUT "sate.out"
#define MAX 30011

struct LISTA {
    long x,c;
    LISTA *next;
} *A[MAX];
struct COADA {
    long x,c;
};
void add(long x, long y, long c) {
    LISTA *tmp = new LISTA;
    tmp -> x = y;
    tmp -> c = c;
    tmp -> next = A[x];
    A[x] = tmp;
}

long S, F, n, m, length;
bool visit[MAX];
COADA Q[MAX];
long p, u;

void p.latime() {
    LISTA *vecin;
    
    p=u=0;
    Q[0].x = S;
    Q[0].c = 0;
    visit[S] = true;
    for (u=p; p<=u; ++p)
        for (vecin=A[Q[p].x]; vecin; vecin=vecin->next)
            if ( !visit[vecin->x] ) {
                Q[++u].x = vecin->x;
                Q[u].c = Q[p].c + vecin->c;
                visit[vecin->x] = true;
                if ( vecin->x == F ) {
                    length = Q[u].c;
                    return;
                }
            }
}

int main() {
    long i, x, y, z;
    
    freopen(FIN, "r", stdin);
    scanf("%ld %ld %ld %ld", &n, &m, &S, &F);
    for (i=0; i<m; ++i) {
        scanf("%ld %ld %ld", &x, &y, &z);
        add(x,y,z);
        add(y,x,-z);
    }
    fclose(stdin);
    
    length = 0;
    if ( S>F ) {
        long aux = S;
        S = F; F = aux;
    }
    p.latime();
    
    freopen(FOUT, "w", stdout);
    printf("%ld\n", length);
    fclose(stdout);
    return 0;
}