Pagini recente » Cod sursa (job #3147607) | Cod sursa (job #2199938) | Cod sursa (job #1298909) | Cod sursa (job #1248972) | Cod sursa (job #2095182)
#include<bits/stdc++.h>
using namespace std;
const int N=50020, INF=9999999999;
vector <pair<int, int> > mda[N];
int main(){
int n, m;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
f>>n>>m;
while(m--){
int x, y, z;
f>>x>>y>>z;
mda[x].push_back({y, z});
}
set <pair<int, int> > h;
h.insert({0,1});
int d[N];
for(int i=2;i<=n;i++)d[i]=INF;
d[1]=0;
while(!h.empty()){
int nod=h.begin()->second;
int s=h.begin()->first;
h.erase(h.begin());
int l=mda[nod].size();
for(int i=0;i<l;i++){
int y=mda[nod][i].first;
int cost=mda[nod][i].second;
if(d[y]>d[nod]+cost){
if(d[y]!=INF){
h.erase(h.find(make_pair(d[y], y)));
}
d[y]=d[nod]+cost;
h.insert(make_pair(d[y], y));
}
}
}
for(int i=2;i<=n;i++)if(d[i]==INF) g<<0<<' '; else g<<d[i]<<' ';
return 0;
}