Pagini recente » Cod sursa (job #2299747) | Cod sursa (job #1279095) | ONIS 2015, Solutii Runda 1 | Monitorul de evaluare | Cod sursa (job #3203882)
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
const int Inf = 1 << 30;
const int Max = 5e4 + 1;
vector <pair<int,int>> graf[Max];
vector <int> d(Max, Inf);
bitset <Max> viz;
priority_queue <pair <int,int>, vector <pair<int,int>>, greater <pair<int,int>>> q;
void dijkstra(int nod)
{
q.push({0, nod});
d[nod] = 0;
while (!q.empty())
{
nod = q.top().second;
q.pop();
if (viz[nod])
continue;
viz[nod] = 1;
for (auto i : graf[nod])
{
if (d[nod] + i.second < d[i.first])
{
d[i.first] = d[nod] + i.second;
q.push({d[i.first], i.first});
}
}
}
}
void read()
{
int x, y, z;
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y >> z;
graf[x].push_back({y, z});
}
}
void print()
{
for (int i = 2; i <= n; ++i)
if (d[i] == Inf)
fout << 0 << " ";
else
fout << d[i] << " ";
}
int main()
{
read();
dijkstra(1);
print();
fin.close();
fout.close();
return 0;
}