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