Pagini recente » Cod sursa (job #151380) | Cod sursa (job #993927) | Cod sursa (job #627566) | Cod sursa (job #2278856) | Cod sursa (job #2283375)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
struct code {
int node;
long long cost ;
int index;
bool operator > (code r) const {
return cost > r . cost;
}
};
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
int const NM = 3e5;
vector <code> v [1 + NM];
long long dp [1 + NM] , k;
bool viz [1 + NM];
priority_queue <code , vector <code> , greater <code> > q;
void shortest (){
q . push ({1 , 0});
while (! q . empty ()){
code x = q . top ();
viz [x . node] = true;
// if (x . index)
// sol . push_back (x . index);
while (q . size () && viz [q . top () . node])
q . pop ();
for(auto i : v [x . node]){
if (dp [i . node] > dp [x . node] + i . cost ){
dp [i . node] = dp [x . node] + i . cost;
q . push ({i . node , dp [i . node] , i . index});
}
}
}
}
int main()
{
int n , m;
cin >> n >> m ;
for(int i = 1 ; i <= m ; ++ i){
int a , b , c;
cin >> a >> b >> c;
v [a] . push_back ({b , c , i});
v [b] . push_back ({a , c , i});
}
fill (dp + 2 , dp + n + 1 , (1LL << 60));
shortest ();
for(int i = 2 ; i <= n ; ++ i)
cout << dp [i] << ' ';
return 0;
}