Pagini recente » Cod sursa (job #978837) | Cod sursa (job #2328641) | Cod sursa (job #1369403) | Cod sursa (job #1304801) | Cod sursa (job #3347571)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
const int mxN = 5e4 + 1;
struct muchie{
int vecin, cost;
};
bool operator>(muchie a, muchie b){
return a.cost > b.cost;
}
int cost[mxN], n, m;
vector<muchie> G[mxN];
priority_queue<muchie, vector<muchie>, greater<muchie>> Q;
int main(){
int a, b, c;
fin >> n >> m;
cost[1] = 1;
for(int i = 1; i <= m; i++){
fin >> a >> b >> c;
G[a].push_back({b, c});
G[b].push_back({a, c});
}
Q.push({1, 1});
while(!Q.empty()){
muchie nod = Q.top();
if(!cost[nod.vecin] || nod.vecin == 1){
cost[nod.vecin] = nod.cost;
for(auto x : G[nod.vecin]){
if(cost[x.vecin] == 0){
Q.push({x.vecin, nod.cost + x.cost});
}
}
}
Q.pop();
}
for(int i = 2; i <= n; i++)
fout << cost[i] - 1 << " ";
}