Pagini recente » Cod sursa (job #777052) | Cod sursa (job #369856) | Cod sursa (job #2254584) | Cod sursa (job #2769080) | Cod sursa (job #2033431)
#include <fstream>
#include <vector>
#include <queue>
int main()
{
std::ifstream in("sate.in");
int n, m, x, y;
in >> n >> m >> x >> y;
auto graph = new std::vector<std::pair<int, int>>[n + 1];
for (auto i = 0; i < m; ++i)
{
int i1, i2, dst;
in >> i1 >> i2 >> dst;
graph[i1].push_back({ i2, dst });
graph[i2].push_back({ i1, dst });
}
auto dst = new int[n + 1];
auto seen = new bool[n + 1];
std::queue<int> que;
que.push(x);
while (!que.empty())
{
auto crr = que.front();
que.pop();
for (auto temp : graph[crr])
if (!seen[temp.first])
{
seen[temp.first] = true;
que.push(temp.first);
dst[temp.first] = ((crr < temp.first) ? (dst[crr] + temp.second) : (dst[crr] - temp.second));
}
}
std::ofstream("sate.out") << dst[y];
}