Pagini recente » Cod sursa (job #3323698) | Cod sursa (job #3300124) | Cod sursa (job #3302841) | Cod sursa (job #1744778) | Cod sursa (job #3312704)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");
#define NMAX 350005
vector<pair<int, int>> graph[NMAX];
int viz[NMAX];
int cost[NMAX];
int solve(int start, int target)
{
queue<int> q;
q.push(start);
while (!q.empty())
{
int x = q.front();
q.pop();
viz[x] = 1;
if (x == target)
{
return cost[x];
}
for (auto i : graph[x])
{
if (viz[i.first])
continue;
cost[i.first] = cost[x] + i.second;
q.push(i.first);
}
}
return -1;
}
int main()
{
int n, m, x, y;
fin >> n >> m >> x >> y;
for (int i = 0; i < m; i++)
{
int a, b, c;
fin >> a >> b >> c;
graph[a].push_back({b, c});
graph[b].push_back({a, -c});
}
fout << solve(x, y);
return 0;
}