#include #include #include #define MAXN 16384 #define MAXM 65536 struct vert { unsigned num_arcs, na; struct arc **arcs; unsigned dist; char done; }; static struct vert verts[MAXN]; struct arc { unsigned f, t, v; } arcs[MAXM]; int main (void) { for (;;) { unsigned n, m, s, c; struct vert *v; struct arc *a; scanf ("%u%u%u%u", &n, &m, &s, &c); if (n == 0 && m == 0 && s == 0 && c == 0) break; for (a = arcs; a < arcs + m; a++) { scanf ("%u%u%u", &a->f, &a->t, &a->v); a->f--; a->t--; verts[a->f].na++; } for (v = verts; v < verts + n; v++) { v->arcs = realloc (v->arcs, sizeof (*v->arcs) * v->na); v->dist = UINT_MAX; v->num_arcs = 0; v->done = 0; } for (a = arcs; a < arcs + m; a++) { v = verts + a->f; v->arcs[v->num_arcs++] = a; } verts[s - 1].dist = 0; while (verts[c - 1].done == 0) { struct vert *max; struct arc **ap; max = NULL; for (v = verts; v < verts + n; v++) { if (v->done == 0 && (max == NULL || max->dist > v->dist)) max = v; } v = max; v->done = 1; for (ap = v->arcs; ap < v->arcs + v->num_arcs; ap++) { struct vert *v2; unsigned val; v2 = verts + (*ap)->t; val = (*ap)->v + v->dist; if (v2->dist > val) v2->dist = val; } } printf ("%u\n", verts[c - 1].dist); } return EXIT_SUCCESS; }