Pagini recente » Cod sursa (job #2512187) | Cod sursa (job #1859260) | Cod sursa (job #2803519) | Cod sursa (job #2301071) | Cod sursa (job #3257877)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 50000;
int n, m;
vector<pair<int, int>> g[NMAX + 1];
int cost[NMAX + 1];
void djk()
{
cost[1] = 0;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push({0, 1});
while(!q.empty())
{
pair<int, int> nod = q.top();
int costc = nod.first;
int nodc = nod.second;
q.pop();
if(cost[nodc] != costc)
continue;
for(int i = 0; i < (int) g[nodc].size(); ++i)
{
int ncost = costc + g[nodc][i].second;
int nnod = g[nodc][i].first;
if(cost[nnod] > ncost)
{
cost[nnod] = ncost;
q.push({ncost, nnod});
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
int x, y, d;
fin >> x >> y >> d;
g[x].push_back({y, d});
}
for(int i = 1; i <= n; ++i)
{
cost[i] = 1e9;
}
djk();
for(int i = 2; i <= n; ++i)
{
if(cost[i] == 1e9)
fout << 0 << " ";
else
fout << cost[i] << " ";
}
return 0;
}