Cod sursa(job #67648)

Utilizator sima_cotizoSima Cotizo sima_cotizo Data 25 iunie 2007 12:58:13
Problema Sate Scor 0
Compilator cpp Status done
Runda preONI 2007, Runda Finala, Clasa a 9-a si gimnaziu Marime 1.19 kb
#include <cstdio>

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

struct LISTA {
	long x,c; 
	LISTA *next;
} *A[MAX];
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];
LISTA Q[MAX];
long p, u;

void push(long x, long c) {
	LISTA *tmp = new LISTA;
	tmp -> x = x;
	tmp -> c = c;
	tmp -> next = 0;
	u -> next = tmp;
	u = tmp;
}

void bf() {
	LISTA *vecin;
	
	p=u=0;
	visit[S] = true;
	for (u=p; p<=u; ++p)
		for (vecin=A[p->x]; vecin; vecin=vecin->next)
			if ( !visit[vecin->x] ) {
				Q[++u].x = vecin->x;
				Q[u].c = 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<n; ++i) {
		scanf("%ld %ld %ld", &x, &y, &z);
		add(x,y,z);
		add(y,x,-z);
	}
	fclose(stdin);
	
	length = -1;
	if ( S>F ) {
		long aux = S;
		S = F; F = aux;
	}
	bf();
	
	freopen(FOUT, "w", stdout);
	printf("%ld\n", length);
	fclose(stdout);
	return 0;
}