Pagini recente » Cod sursa (job #1758639) | Cod sursa (job #2834833) | Cod sursa (job #235921) | Cod sursa (job #169223) | Cod sursa (job #1375741)
#include <cstring>
#include <fstream>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
int N, M;
vector<pair<int, int> > V[50002];
priority_queue<pair<int, int> > Q;
int D[50002];
int main()
{
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
fin >> N >> M;
for (int i = 1, nod1, nod2, cost; i <= M; ++i)
{
fin >> nod1 >> nod2 >> cost;
V[nod1].push_back(make_pair(nod2, cost));
}
for (int i = 1; i <= N; ++i)
D[i] = INF;
D[1] = 0;
Q.push(make_pair(0, 1));
while (!Q.empty())
{
pair<int, int> now = Q.top();
Q.pop();
if (-now.first != D[now.second]) continue;
for (auto it = V[now.second].begin(); it != V[now.second].end(); ++it)
if (D[now.second] + it->second < D[it->first])
{
D[it->first] = D[now.second] + it->second;
Q.push(make_pair(-D[it->first], it->first));
}
}
for (int i = 2; i <= N; ++i)
{
if (D[i] == INF) D[i] = 0;
fout << D[i] << ' ';
}
fout << '\n';
fin.close();
fout.close();
}