Pagini recente » Clasament acs_pc_2017-2018_winter_break_12314132 | Cod sursa (job #2665466) | Cod sursa (job #2024236) | Cod sursa (job #210037) | Cod sursa (job #1128738)
#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;
bool vizitat;
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) {
for(int j = 0; j < g[x.b].vecini.size(); j++) {
int vecin = g[x.b].vecini[j].b;
if (!g[vecin].vizitat) {
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});
}
coada.push({0, 1});
while(!coada.empty()) {
muchie aux = coada.top();
coada.pop();
if(g[aux.b].vizitat == false) {
g[aux.b].cost = aux.cost;
g[aux.b].vizitat = true;
dij(aux);
}
}
for(i = 2; i <= n; i++) {
fout << g[i].cost << ' ';
}
fin.close();
fout.close();
return 0;
}