Pagini recente » Cod sursa (job #1625536) | Cod sursa (job #2233880) | Cod sursa (job #167431) | Cod sursa (job #793764) | Cod sursa (job #2369894)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
int viz[50001], d[50001];
const int INF=1e9;
vector<pair<int, int>>G[50001];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>PQ;
int main()
{
fin>>n>>m;
for(int i=1; i<=m; ++i)
{
int x, y, c;
fin>>x>>y>>c;
G[x].push_back(make_pair(y, c));
}
for(int i=1; i<=n; ++i) d[i]=INF;
d[1]=0;
PQ.push(make_pair(0, 1));
while(!PQ.empty())
{
pair<int, int> nod=PQ.top();
PQ.pop();
if(!viz[nod.second])
{
for(auto it:G[nod.second])
{
if(d[it.first]>d[nod.second]+it.second)
{
d[it.first]=d[nod.second]+it.second;
PQ.push(make_pair(d[it.first], it.first));
}
}
}
viz[nod.second]=1;
}
for(int i=2; i<=n; ++i) fout<<d[i]<<" ";
fout<<"\n";
return 0;
}