Cod sursa(job #67999)

Utilizator sima_cotizoSima Cotizo sima_cotizo Data 26 iunie 2007 11:26:18
Problema Sate Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 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 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;
	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<n; ++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;
	}
	bf();
	
	freopen(FOUT, "w", stdout);
	printf("%ld\n", length);
	fclose(stdout);
	return 0;
}