Pagini recente » Cod sursa (job #1329131) | Cod sursa (job #1314240) | Cod sursa (job #3330245) | Cod sursa (job #1247878) | Cod sursa (job #3307075)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX=5e4, inf=1e9;
vector<pair<int,int> > mat[NMAX+5];
vector<int> dist(NMAX+5, inf);
void dijkstra(){
priority_queue<pair<int,int> > q;
q.push({0, 1});
dist[1]=0;
while(!q.empty()){
int nod=q.top().second;
q.pop();
for(auto it:mat[nod]){
if(dist[it.first]>dist[nod]+it.second){
dist[it.first]=dist[nod]+it.second;
q.push({-dist[it.first], it.first});
}
}
}
}
int main(){
int n, m;
fin>>n>>m;
for(int i=1;i<=m;i++){
int x, y, z;
fin>>x>>y>>z;
mat[x].push_back({y,z});
}
dijkstra();
for(int i=2;i<=n;i++){
fout<<dist[i]<<' ';
}
}