Pagini recente » Cod sursa (job #2745728) | Cod sursa (job #2733762) | Cod sursa (job #709187) | Cod sursa (job #3265670) | Cod sursa (job #1128603)
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int INF = 1000000000;
struct muchie {
int cost;
int b;
};
struct nod {
int cost;
vector<muchie> vecini;
};
int n, m, i, x, y, c;
nod g[50001];
priority_queue<muchie> coada;
bool operator < (const muchie &a, const muchie &b) {
return a.cost > b.cost;
}
void dij(muchie x) {
g[x.b].cost = x.cost;
for(int j = 0; j < g[x.b].vecini.size(); j++) {
int vecin = g[x.b].vecini[j].b;
if (g[vecin].cost > g[x.b].cost + g[x.b].vecini[j].cost) {
coada.push({g[x.b].cost + g[x.b].vecini[j].cost, vecin});
}
}
}
int main() {
fin >> n >> m;
for(i = 0; i < m; i++) {
fin >> x >> y >> c;
g[x].vecini.push_back({c, y});
}
for(i = 1; i <= n; i++) {
g[i].cost = INF;
}
coada.push({0, 1});
while(!coada.empty()) {
muchie aux = coada.top();
coada.pop();
if(g[aux.b].cost == INF) {
dij(aux);
}
}
for(i = 2; i <= n; i++) {
fout << g[i].cost << ' ';
}
fin.close();
fout.close();
return 0;
}