Pagini recente » Cod sursa (job #2594415) | Cod sursa (job #2624081) | Cod sursa (job #253659) | Cod sursa (job #1881751) | Cod sursa (job #2827762)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>
#define nod first
#define cost second
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int N = 5 * 1e4 + 1, INF = 1e9 + 1;
vector <pair<int, int>> adj[N];
bitset <N> viz;
vector <int> costuri(N, INF);
struct my_compare {
bool operator () (int x, int y) {
return x > y;
}
};
priority_queue <int, vector<int>, my_compare> pq;
void djk(int nod){
pq.push(nod);
viz[nod] = 1;
costuri[nod] = 0;
while(!pq.empty()){
int next_nod = pq.top();
pq.pop();
viz[next_nod] = 0;
for(auto vec : adj[next_nod]){
if(costuri[vec.nod] > costuri[next_nod] + vec.cost){
costuri[vec.nod] = costuri[next_nod] + vec.cost;
if(!viz[vec.nod]){
pq.push(vec.nod);
viz[vec.nod] = 1;
}
}
}
}
}
int main() {
int n, m;
fin >> n >> m;
for(int i = 0; i < m; i++){
int a, b, c;
fin >> a >> b >> c;
adj[a].push_back({b, c});
}
int src = 1;
djk(src);
for(int i = 2; i <= n; i++) costuri[i] != INF ? fout << costuri[i] << ' ' : fout << 0 << ' ';
return 0;
}