Pagini recente » Cod sursa (job #53515) | Cod sursa (job #264253) | Cod sursa (job #2091347) | Cod sursa (job #3155421) | Cod sursa (job #2325808)
#include <set>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m, i, j, x, y, c, k, d[50100], v[50100], minim, nod2, cost, nod;
vector< pair<int,int> >a[50100];
set< pair<int,int> >sett;
const int inf=(1<<30);
int main(){
fin>>n>>m;
for (i=1; i<=m; i++){
fin>>x>>y>>c;
a[x].push_back(make_pair(y, c));
}
for(i=2; i<=n; i++){
d[i]=inf;
}
sett.insert(make_pair(0,1));
while(!sett.empty()){
nod=sett.begin()->second;
sett.erase(sett.begin());
for(i=0; i<a[nod].size();i++){
if(a[nod][i].second+d[nod]<d[a[nod][i].first]){
sett.erase({d[a[nod][i].first],a[nod][i].first});
d[a[nod][i].first]=a[nod][i].second+d[nod];
sett.insert({d[a[nod][i].first],a[nod][i].first});
}
}
}
for(i=2; i<=n; i++){
if(d[i]!=inf){
fout<<d[i]<<" ";
}else{
fout<<0<<" ";
}
}
}