Pagini recente » Cod sursa (job #1328708) | Cod sursa (job #1656693) | Cod sursa (job #923262) | Cod sursa (job #527085) | Cod sursa (job #2047470)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
class cmp{
public:
bool operator() (pair<int , int> & a , pair<int , int> & b){
return a.second > b.second;
}
};
priority_queue<pair<int,int> , vector<pair<int , int>> , cmp> Q;
int dp[50100];
vector < vector <pair<int , int> > > gr(50100);
int main() {
int n , m;
cin>>n>>m;
for (int i=2; i<=n; i++){
dp[i] = 1000000000;
}
for (int i=1; i<=m; i++){
int a , b , val;
cin>>a>>b>>val;
gr[a].push_back({b , val});
}
Q.push({1 , 0});
while (!Q.empty()){
pair<int , int> now = Q.top();
Q.pop();
if (now.second != dp[now.first]){
continue;
}
for (auto &x : gr[now.first]){
if (dp[x.first] > now.second + x.second){
dp[x.first] = now.second + x.second;
Q.push({x.first , dp[x.first]});
}
}
}
for (int i=2; i<=n; i++){
if (dp[i] == 1000000000){
cout<<0<<" ";
}
else{
cout<<dp[i]<<" ";
}
}
return 0;
}