Pagini recente » Cod sursa (job #475697) | Cod sursa (job #1327454) | Cod sursa (job #2358709) | Cod sursa (job #1112365) | Cod sursa (job #3133599)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int inf = (1 << 30);
int d[100001], n, p, m;
bool viz[100001];
priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>> > q;
vector<pair<int, int>> a[100001];
void dijkstra(int nod)
{
for (int i = 1; i <= n; i++)
d[i] = inf;
q.push({ 0,nod });
d[nod] = 0;
while (!q.empty())
{
int nodcurr = q.top().second;
q.pop();
if (viz[nodcurr] == 0)
{
viz[nodcurr] = 1;
for (unsigned int i = 0; i < a[nodcurr].size(); i++)
{
int nextnod = a[nodcurr][i].first;
int cost = a[nodcurr][i].second;
if (d[nodcurr] + cost < d[nextnod])
{
d[nextnod] = d[nodcurr] + cost;
q.push({ d[nextnod],nextnod });
}
}
}
}
}
int main()
{
int cost, x, y;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> x >> y >> cost;
a[x].push_back({ y,cost });
}
dijkstra(1);
for (int i = 2; i <= n; i++)
{
if (d[i] != inf)
cout << d[i] << ' ';
else cout << 0 << ' ';
}
}