Pagini recente » Cod sursa (job #2350402) | Cod sursa (job #600383) | Cod sursa (job #2252132) | Cod sursa (job #3263057) | Cod sursa (job #2762055)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
int n,m;
vector <pair<long long , long long>> a[250005];
long long dist[250005];
typedef pair<long long, int> pi;
priority_queue<pi, vector<pi>, greater<pi> > Q;
void Dijkstra()
{
Q.push({0,1});
while(!Q.empty())
{
long long val=Q.top().first;
int i=Q.top().second;
for(int x=0; x<a[i].size() ; ++x)
{
if((dist[i]+a[i][x].second<dist[a[i][x].first] && a[i][x].first!=1 ) || (dist[a[i][x].first]==0 && a[i][x].first!=1))
{
dist[a[i][x].first]=dist[i]+a[i][x].second;
Q.push({dist[a[i][x].first] , a[i][x].first});
}
}
Q.pop();
}
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;++i)
{
int x,y,cost;
f>>x>>y>>cost;
a[x].push_back({y ,cost});
}
Dijkstra();
for(int i=2;i<=n;++i)
g<<dist[i]<<" ";
return 0;
}