Pagini recente » Cod sursa (job #2978686) | Cod sursa (job #3031683) | Cod sursa (job #2047053) | Cod sursa (job #2162344) | Cod sursa (job #2186036)
#include <bits/stdc++.h>
using namespace std;
int dist[50100];
set<pair<int, int> > q;
vector<pair<int, int> > v[50001];
int main()
{
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, z;
cin >> x >> y >> z;
v[x].push_back({y, z});
v[y].push_back({x, z});
}
for (int i = 2; i <= n; i++)
dist[i] = 443446554;
q.insert({0, 1});
while (!q.empty())
{
int c = q.begin()->second;
int d = q.begin()->first;
q.erase(q.begin());
int p = v[c].size();
for (int i = 0; i < p; i++)
{
int destination = v[c][i].first;
int weight = v[c][i].second;
if (dist[c] + v[c][i].second < dist[destination])
{
if(dist[destination] != 443446554)
q.erase(q.find({dist[destination], destination}));
dist[destination] = dist[c] + weight;
q.insert({dist[destination], destination});
}
}
}
for (int i = 2; i <= n; i++)
cout << dist[i] << ' ';
}