Pagini recente » Cod sursa (job #1647891) | Cod sursa (job #1295113) | Cod sursa (job #337254) | Cod sursa (job #1295362) | Cod sursa (job #3042250)
#include <fstream>
#include <queue>
using namespace std;
const int NMAX = 30000;
vector<pair<int, int>> graf[1 + NMAX];
int sol[1 + NMAX];
queue<int> q;
bool vizitat[1 + NMAX];
int main()
{
ifstream in("sate.in");
ofstream out("sate.out");
ios_base::sync_with_stdio(false);
in.tie(nullptr);
int n, m, x, y;
in >> n >> m >> x >> y;
for (int i = 1; i <= m; i++)
{
int a, b, d;
in >> a >> b >> d;
if (a > b)
swap(a, b);
graf[a].emplace_back(b, d);
graf[b].emplace_back(a, -d);
}
if (x > y)
swap(x, y);
q.emplace(x);
while (!q.empty())
{
int nod = q.front();
q.pop();
vizitat[nod] = true;
for (int i = 0; i < graf[nod].size(); i++)
{
if (!vizitat[graf[nod][i].first])
{
sol[graf[nod][i].first] = sol[nod] + graf[nod][i].second;
q.emplace(graf[nod][i].first);
}
}
}
out << sol[y] << '\n';
in.close();
out.close();
return 0;
}