Pagini recente » Cod sursa (job #2468799) | Cod sursa (job #2818333) | Cod sursa (job #700318) | Cod sursa (job #2851678) | Cod sursa (job #3238173)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 50005;
vector<pair<int,int>> graph[MAX];
bitset<MAX> visited;
vector<int> dist;
void dijkstra(int start)
{
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
q.push({0, start});
dist[start] = 0;
while (!q.empty())
{
pair<int,int> node = q.top();
q.pop();
if (!visited[node.second])
{
visited[node.second] = 1;
for (pair<int,int> next : graph[node.second])
{
if (dist[next.first] > dist[node.second] + next.second)
{
dist[next.first] = dist[node.second] + next.second;
q.push({dist[next.first], next.first});
}
}
}
}
}
int main()
{
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int n, m;
cin >> n >> m;
dist.assign(n+1, INT_MAX);
for (int i = 1; i <= m; ++i)
{
int x, y, cost;
cin >> x >> y >> cost;
graph[x].push_back({y, cost});
}
dijkstra(1);
for (int i = 2; i <= n; ++i)
cout << dist[i] << " ";
return 0;
}