Pagini recente » Cod sursa (job #66812) | Cod sursa (job #3005155) | Cod sursa (job #1080014) | Cod sursa (job #1851578) | Cod sursa (job #1020558)
#include <cstdio>
#include <vector>
#include <queue>
#define NMAX 30005
using namespace std;
FILE *fin, *fout;
struct varf {
short int vf;
int c;
}aux;
vector <varf> graf[NMAX];
queue <short int> coada;
long dist[NMAX];
int main()
{
int n, m, i, xx, yy, lg;
short int vf1, vf2;
bool ready=0;
fin=fopen("sate.in", "r"); fout=fopen("sate.out", "w");
fscanf(fin, "%d%d%d%d", &n, &m, &xx, &yy);
for (i=1; i<=m; ++i) {
fscanf (fin, "%hd%hd%d", &vf1, &vf2, &aux.c);
aux.vf=vf1; graf[vf2].push_back(aux);
aux.vf=vf2; graf[vf1].push_back(aux);
}
//BFS
coada.push(xx); dist[xx]=1;
while (!coada.empty() && !ready) {
vf1=coada.front(); coada.pop();
lg=graf[vf1].size();
for (i=0; i<lg; ++i) if (!dist[graf[vf1][i].vf]) {
vf2=graf[vf1][i].vf;
if (vf2>vf1)
dist[vf2]=dist[vf1]+graf[vf1][i].c;
else
dist[vf2]=dist[vf1]-graf[vf1][i].c;
coada.push(graf[vf1][i].vf);
if (graf[vf1][i].vf==yy) {
ready=1;
break;
}
}
}
fprintf(fout, "%ld\n", dist[yy]-1);
fclose(fin); fclose(fout);
return 0;
}