Pagini recente » Cod sursa (job #1698111) | Cod sursa (job #2519548) | Cod sursa (job #3127145) | Cod sursa (job #2403430) | Cod sursa (job #3252183)
#include<iostream>
#include<vector>
#include<fstream>
#include<climits>
#include<queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int nmax = 50005;
int n,m;
bool vizitat[nmax];
vector<pair<int,int>> g[nmax];
vector<long> cost(nmax,INT_MAX);
priority_queue<pair<int,int>> q;
void bfs(int nod){
q.push(make_pair(0,1));
while(!q.empty()){
pair<int,int> u = q.top();
int x = u.second;
for(pair<int,int> vec : g[x]){
if(cost[vec.first] > cost[x] + vec.second){
cost[vec.first] = cost[x] + vec.second;
q.push(make_pair(-cost[vec.first],vec.first));
}
}
q.pop();
}
}
int main(){
fin>>n>>m;
for(int i = 1; i <= m; i++){
int x,y,z;
fin>>x>>y>>z;
g[x].push_back(make_pair(y,z));
}
cost[1] = 0;
bfs(1);
for(int i = 2; i <= n; i++)if(cost[i] == INT_MAX)fout<<0<<" ";
else fout<<cost[i]<<" ";
}