Pagini recente » Cod sursa (job #259694) | Cod sursa (job #626099) | Cod sursa (job #48215) | Cod sursa (job #2114719) | Cod sursa (job #2513355)
#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;
int viz[nmax];
queue <int> q;
vector < pair < int,int > > muchii[nmax];
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({b,c});
muchii[b].push_back({a,c});
}
viz[x] = 1;
q.push(x);
while (!q.empty())
{
int nod = q.front();
q.pop();
for (auto v : muchii[nod])
{
if (!viz[v.first])
{
viz[v.first] = 1;
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)
{
fout << d[y];
return 0;
}
}
}
}
fout << d[y];
return 0;
}