Pagini recente » Cod sursa (job #1597929) | Cod sursa (job #248367) | Cod sursa (job #3224312) | Cod sursa (job #394874) | Cod sursa (job #1204618)
using namespace std;
#include <fstream>
#include <queue>
ifstream fin("sate.in");
ofstream fout("sate.out");
const int Nmax = 30000;
struct lista {int first, second; lista* urm;} ;
int d[Nmax+1];
lista* v[Nmax+1];
queue<int> Q;
int mod2(int a) {return a > 0 ? a : (-a);}
void push1(int, int, int) ;
int main()
{
int i, m, n, a, b, c, x, y, vf;
lista *nod;
fin >> n >> m >> x >> y;
for(; m; --m)
{
fin >> a >> b >> c;
push1(a, b, c);
push1(b, a, c);
}
d[x] = 0;
for(Q.push(x); !Q.empty(); Q.pop())
{
vf = Q.front();
for(nod = v[vf]; nod != NULL; nod = nod->urm)
if((nod->first) != x && d[nod->first] == 0)
{
if(vf < nod->first) d[nod->first] = d[vf] + (nod->second);
else d[nod->first] = d[vf] - (nod->second);
Q.push(nod->first);
if((nod->first) == y) {fout << mod2(d[y]) << '\n'; return 0;}
}
}
return 0;
}
void push1(int a, int b, int c)
{
lista *nou, *nod;
nou = new lista; nou->first = b; nou->second = c; nou->urm = NULL;
if(v[a] == NULL) {v[a] = nou; return;}
for(nod = v[a]; nod->urm != NULL; nod = nod->urm) ;
nod->urm = nou;
}