Pagini recente » Cod sursa (job #2186928) | Cod sursa (job #3288231) | Cod sursa (job #3037893) | Cod sursa (job #2885996) | Cod sursa (job #2033426)
#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];
}