Pagini recente » Cod sursa (job #2066559) | Cod sursa (job #1134905) | Cod sursa (job #437185) | Cod sursa (job #878496) | Cod sursa (job #2914689)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in ("sate.in");
ofstream out ("sate.out");
const int max_size = 3e4 + 1, INF = 1e9 + 1;
int d[max_size], viz[max_size];
vector <pair <int, int>> edges[max_size];
struct cmp{
bool operator()(int x, int y)
{
return d[x] > d[y];
}
};
priority_queue <int, vector <int>, cmp> pq;
void djk (int nod)
{
viz[nod] = 1;
d[nod] = 0;
pq.push(nod);
while (!pq.empty())
{
nod = pq.top();
pq.pop();
viz[nod] = 0;
for (auto f : edges[nod])
{
if (d[nod] + f.second < d[f.first])
{
d[f.first] = d[nod] + f.second;
if (viz[f.first] == 0)
{
viz[f.first] = 1;
pq.push(f.first);
}
}
}
}
}
int main ()
{
int n, t, x, y;
in >> n >> t >> x >> y;
while (t--)
{
int a, b, c;
in >> a >> b >> c;
edges[a].push_back({b, c});
edges[b].push_back({a, -c});
}
for (int i = 1; i <= n; i++)
{
d[i] = INF;
}
djk(x);
out << d[y];
in.close();
out.close();
return 0;
}