Pagini recente » Cod sursa (job #2689959) | Cod sursa (job #92045) | Cod sursa (job #1065598) | Cod sursa (job #1265299) | Cod sursa (job #2373997)
#include <fstream>
#include <queue>
using namespace std;
ifstream fin ("sate.in");
ofstream fout ("sate.out");
void DFS(int x);
void ReadFunction();
const int maxn = 30001;
int n, m, x, y, d[maxn];
vector<vector<pair<int, int>>> G;
bool v[maxn];
int main()
{
ReadFunction();
DFS(x);
fout << d[y];
}
void DFS(int x)
{
int dist = 0;
queue<int> q;
q.push(x);
v[x] = true;
int nod;
while (!q.empty())
{
nod = q.front();
q.pop();
for (const auto& p : G[nod])
{
if (!v[p.first])
{
if (nod < p.first)
d[p.first] = d[nod] + p.second;
else
d[p.first] = d[nod] - p.second;
q.push(p.first);
v[p.first] = true;
}
}
}
}
void ReadFunction()
{
int a,b, d;
fin >> n >> m >> x >> y;
G = vector<vector<pair<int, int>>>(n + 1);
for (int i = 1; i <= m; ++i)
{
fin >> a>> b >> d;
G[a].push_back({b, d});
G[b].push_back({a, d});
}
}