Pagini recente » Cod sursa (job #943359) | Cod sursa (job #1362168) | Istoria paginii runda/concurs_000002 | Cod sursa (job #2802746) | Cod sursa (job #1701748)
#include <iostream>
#include <fstream>
#include <list>
#include <unordered_map>
using namespace std;
typedef pair<short,int> paer;
struct nod {
paer p;
nod *next;
};
struct sNod {
short sat;
sNod *next;
};
int main()
{
ifstream in("sate.in");
ofstream out("sate.out");
unordered_map<short,nod*> vecini;
unordered_map<short,int> distante;
short n, x, y, a, b;
int d, i, m;
in>>n>>m>>x>>y;
for (i=0; i<m; i++) {
in>>a>>b>>d;
nod *l = new nod;
l->p = paer(b, d);
l->next = vecini[a];
vecini[a] = l;
l = new nod;
l->p = paer(a, d);
l->next = vecini[b];
vecini[b] = l;
}
sNod *bf = new sNod;
bf->sat = x;
while (bf) {
short sat = bf->sat;
d = distante[sat];
bf = bf->next;
nod *l = vecini[sat];
while (l) {
paer p = l->p;
if (!distante[p.first]) {
if (sat > p.first) {
distante[p.first] = d - p.second;
}
else {
distante[p.first] = d + p.second;
}
if (p.first == y) {
goto sol;
}
sNod *sn = new sNod;
sn->sat = p.first;
sn->next = bf;
bf = sn;
}
l = l->next;
}
}
sol:
out<<distante[y];
in.close();
out.close();
return 0;
}