Pagini recente » Cod sursa (job #2498850) | Cod sursa (job #1291320) | Istoria paginii utilizator/georgiana1904 | Profil hikaru.ari | Cod sursa (job #1475320)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
int distmin[30001];
bool inheap[30001];
bool cc(int& a, int& b)
{
if (distmin[a] <= distmin[b]) { return false; }
return true;
}
int main()
{
ifstream in("sate.in");
ofstream out("sate.out");
register int i, n, m, x, y, a, b, c;
register vector<pair<int, int> > d[30001];
register vector<int> gramada;
int tot = 0;
in >> n;
in >> m;
in >> x;
in >> y;
for (i = 1;i <= m;i++)
{
in >> a;
in >> b;
in >> c;
d[a].push_back(make_pair(b, c));
d[b].push_back(make_pair(a, -c));
}
for (i = 1;i <= n;i++)
{
distmin[i] = 2000000000;
}
distmin[x] = 0;
inheap[x] = true;
gramada.push_back(x);
while (!gramada.empty())
{
tot++;
if (tot == 20000&&m>40000) { break; }
b = gramada[0];
pop_heap(gramada.begin(), gramada.end(), cc);
inheap[b] = false;
gramada.pop_back();
for (i = 0;i < d[b].size();i++)
{
if (distmin[d[b][i].first]>distmin[b] + d[b][i].second)
{
distmin[d[b][i].first] = distmin[b] + d[b][i].second;
if (inheap[d[b][i].first] == false && d[b][i].first != y)
{
inheap[d[b][i].first] = true;
gramada.push_back(d[b][i].first);
push_heap(gramada.begin(), gramada.end(), cc);
}
}
}
}
out << distmin[y];
}