Pagini recente » Cod sursa (job #2603471) | Cod sursa (job #3232900) | Cod sursa (job #492527) | Cod sursa (job #924374) | Cod sursa (job #3305595)
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int dist[100005];
struct comp{
bool operator()(int a, int b){
return dist[a]>dist[b];
}
};
priority_queue<int, vector<int>, comp> pq;
int n, m, x, y, c;
vector<pair<int, int>> adj[100005];
bool inQ[100005];
void dijkstra(int nod){
dist[nod]=1;
pq.push(nod);
inQ[nod]=true;
while(!pq.empty()){
int nodCrt=pq.top();
pq.pop();
for(auto vec:adj[nodCrt]){
int cost=dist[nodCrt]+vec.second;
if(dist[vec.first]==0||cost<dist[vec.first]){
dist[vec.first]=cost;
if(!inQ[vec.first]){
inQ[vec.first]=true;
pq.push(vec.first);
}
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
cin>>x>>y>>c;
adj[x].push_back({y, c});
}
dijkstra(1);
for(int i=2;i<=n;i++){
if(dist[i]!=0){
cout<<dist[i]-1<<" ";
}
else{
cout<<0<<" ";
}
}
}