Pagini recente » Cod sursa (job #3320274) | Cod sursa (job #3318477) | Cod sursa (job #1856271) | Cod sursa (job #3133478) | Cod sursa (job #3344334)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define NMAX 50005
vector<pair<int,int>> g[NMAX];
int dist[NMAX];
int oo = (1<<30);
struct State{
int i,c;
bool operator<(const State& other) const{
return c>other.c;
}
};
void dijkstra(int n){
priority_queue<State> q;
for(int i=0;i<NMAX;i++){
dist[i]=oo;
}
q.push({n,0});
while(!q.empty()){
int x = q.top().i;
int c = q.top().c;
q.pop();
if(dist[x]!=oo){
continue;
}
dist[x]=c;
for(auto i : g[x]){
if(dist[i.first]==oo){
q.push({i.first,i.second + c});
}
}
}
}
int main()
{
int n,m;
fin>>n>>m;
for(int i=0;i<m;i++){
int a,b,c;
fin>>a>>b>>c;
g[a].push_back({b,c});
}
dijkstra(1);
for(int i=2;i<=n;i++){
if(dist[i]!=oo)
fout<<dist[i]<<" ";
else{
fout<<0<<" ";
}
}
return 0;
}