Pagini recente » Cod sursa (job #953729) | Cod sursa (job #1954413) | Cod sursa (job #113741) | Cod sursa (job #1520711) | Cod sursa (job #3250667)
#include <fstream>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int INF = 0x3F3F3F3F;
int n, m, x, y, z;
vector<vector<pair<int, int>>> graf;
vector<int> d, viz;
void djikstra(int nod)
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push({0, nod});
while(!q.empty())
{
nod = q.top().second;
q.pop();
if(!viz[nod])
{
for(auto next:graf[nod])
if(d[nod] + next.second < d[next.first])
{
d[next.first] = d[nod] + next.second;
q.push({d[next.first], next.first});
}
viz[nod] = 1;
}
}
}
int main()
{
cin >> n >> m;
graf.assign(n+1,vector<pair<int, int>>());
d.assign(n+1, INF);
viz.resize(n+1);
while(m--)
{
cin >> x >> y >> z;
graf[x].push_back({y, z});
}
d[1] = 0;
djikstra(1);
for(int i=2; i<=n; i++)
if(d[i] == INF)
cout << 0 << " ";
else
cout << d[i] << " ";
return 0;
}