Pagini recente » Cod sursa (job #2307868) | Cod sursa (job #1042982) | Cod sursa (job #1264759) | Cod sursa (job #28861) | Cod sursa (job #2513352)
#include <bits/stdc++.h>
#define nmax 30001
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
int d[nmax];
int n, m;
int x, y;
bool viz[nmax];
queue <int> q;
vector < pair < int,int > > muchii[nmax];
inline void bfs()
{
viz[x] = true;
q.push(x);
while (!q.empty())
{
int nod = q.front();
q.pop();
for (auto v : muchii[nod])
{
if (!viz[v.first])
{
viz[v.first] = true;
q.push(v.first);
if (v.first > nod)
d[v.first] = d[nod] + v.second;
else
d[v.first] = d[nod] - v.second;
if (v.first == y)
return;
}
}
}
}
int main()
{
fin >> n >> m >> x >> y;
for (int i = 1; i<= m; i++)
{
int a, b, c;
fin >> a >> b >> c;
muchii[a].push_back(make_pair(b,c));
muchii[b].push_back(make_pair(a,c));
}
bfs();
fout << d[y];
return 0;
}