Pagini recente » Cod sursa (job #141950) | Cod sursa (job #2136213) | Cod sursa (job #2772116) | Cod sursa (job #2757793) | Cod sursa (job #2695399)
#include <iostream>
#include<vector>
#include<queue>
#include<fstream>
#define inf 2e9
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,x,y,price,D[50010],node;
priority_queue<pair<int,int> >q;
vector<pair <int,int> >V[50010];
void dijk(){
q.push({0,1});
D[1]=0;
while(!q.empty()){
node=q.top().second;
price=-q.top().first;
q.pop();
if(D[node]!=0 && D[node]!=inf){
continue;
}
D[node]=price;
for(auto i:V[node]){
if(D[i.second]>price+i.first){
q.push({-(price+i.first),i.second});
}
}
}
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;i++){
f>>x>>y>>price;
V[x].push_back({price,y});
}
for(int i=2;i<=n;i++)D[i]=inf;
dijk();
for(int i=2;i<=n;i++){if(D[i]!=inf)
g<<D[i]<<' ';
else g<<0<<' ';}
}