Pagini recente » Cod sursa (job #408780) | Cod sursa (job #1520458) | Cod sursa (job #626979) | Cod sursa (job #425061) | Cod sursa (job #3201807)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int inf=1e9;
int n,m,i,x,y,c,dist[50005],l,fr[50005];
vector< pair<int,int> > G[50005];
priority_queue< pair<int,int> > q;
//sorteaza dupa primul termen si la egalitate dupa al doilea
void dijkstra(int k){
for(i=1;i<=n;++i) dist[i]=inf;
q.push(make_pair(0,k) );
dist[k]=0;
while(!q.empty()){
k=q.top().second;
l=q.top().first;
q.pop();
//if(0-l==dist[k])//verific daca modul curent de a ajunge la nodul curent e cel mai bun
if(fr[k]==0)
for(auto i: G[k])
if(dist[k]+ i.second < dist[ i.first ]){
dist[ i.first ]= dist[k]+ i.second;
q.push( make_pair(-1*dist[i.first] , i.first) );
}
}
fr[k]=1;
}
int main()
{
f>>n>>m;
for(i=1;i<=m;++i){
f>>x>>y>>c;
G[x].push_back( make_pair(y,c) );
}
dijkstra(1);
for(i=2;i<=n;++i){
if(dist[i]==inf) g<<0<<' ';
else g<<dist[i]<<' ';
}
return 0;
}