Pagini recente » Monitorul de evaluare | Cod sursa (job #3269848) | Cod sursa (job #1895683) | Cod sursa (job #1703543) | Cod sursa (job #3307076)
#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++){
if(dist[i]==inf){
fout<<0<<' ';
}else{
fout<<dist[i]<<' ';
}
}
}