Cod sursa(job #215406)

Utilizator alex_mircescuAlex Mircescu alex_mircescu Data 18 octombrie 2008 16:23:04
Problema Sate Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <stdio.h>
#include <math.h>

#define maxm 1024*128*2
#define maxn 32*1024

long n, m, x, i, next[maxm], v[maxm], first[maxn], sel[maxn], Y, t[maxm];

long df(long a, long b) {
	long x, y;
	if (a == Y) {
		return b;
	}
	sel[a] = 1;
	x = first[a];
	while (x) {
		y = v[x];
		if (sel[y]) {
			x = next[x];
			continue;
		}
		long val = t[(x + 1) / 2], p;
		if (y < a) {
			p = df(y, b - val);
		} else {
			p = df(y, b + val);
		}
		if (p) {
			return p;
		}
		x = next[x];
	}
	return 0;
}

int main() {
	freopen("sate.in", "r", stdin);
	freopen("sate.out", "w", stdout);
	scanf("%ld %ld %ld %ld", &n, &m, &x, &Y);
	for (i = 1; i <= m; ++i) {
		scanf("%ld %ld %ld", &v[i * 2 - 1], &v[i * 2], &t[i]);
		long a = v[i * 2 - 1], b = v[i * 2];
		next[i * 2] = first[a];
		next[i * 2 - 1] = first[b];
		first[a] = i * 2;
		first[b] = i * 2 - 1;
	}
	printf("%ld\n", df(x, 0));
}