Pagini recente » Cod sursa (job #2267673) | Cod sursa (job #1358844) | Cod sursa (job #1880508) | Cod sursa (job #2423828) | Cod sursa (job #68053)
Cod sursa(job #68053)
#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<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;
}
bf();
freopen(FOUT, "w", stdout);
printf("%ld\n", length);
fclose(stdout);
return 0;
}