Pagini recente » Cod sursa (job #850142) | Cod sursa (job #3354735) | Cod sursa (job #873277) | Cod sursa (job #2175123) | Cod sursa (job #3312178)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m;
vector<pair<int,int>>L[50002];
int dist[50002];
void Dijkstra()
{
for(int i=1;i<=n;++i)
dist[i]=2e9;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> >pq;
pq.push({0,1});
dist[1]=0;
while(!pq.empty())
{
int nod=pq.top().second;
int c=pq.top().first;
pq.pop();
if(dist[nod]<c)
continue;
for(auto it : L[nod])
{
if(dist[it.first]> dist[nod]+ it.second)
{
dist[it.first]=dist[nod]+it.second;
pq.push({dist[it.first],it.first});
}
}
}
for(int i=2;i<=n;++i)
{
if(dist[i]==2e9)
fout<<0<<' ';
else
fout<<dist[i]<<' ';
}
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;++i)
{
int x,y,c;
fin>>x>>y>>c;
L[x].push_back({y,c});
}
Dijkstra();
return 0;
}