Pagini recente » Cod sursa (job #2947032) | Cod sursa (job #2299940) | Cod sursa (job #941437) | Cod sursa (job #2679129) | Cod sursa (job #2816299)
#include <fstream>
#include <vector>
#include <set>
#include <cfloat>
#include <cmath>
using namespace std;
ifstream f("dmin.in");
ofstream g("dmin.out");
#define NMAX 10000
#define INF (DBL_MAX/2)
#define MOD 104659
#define TIP pair<long double,int>
#define MARJA 0.000001
int n, m, ct[NMAX];
long double d[NMAX];
vector<pair<int, int>> G[NMAX];
set<TIP > q;
void citire() {
f >> n >> m;
int x, y, t;
for (int i = 1; i <= m; ++i) {
f >> x >> y >> t;
long double cost = log2((long double) t);
G[x].push_back({cost, y});
G[y].push_back({cost, x});
}
}
void initializare() {
d[1] = 0;
ct[1] = 1;
for (int x = 2; x <= n; ++x)
d[x] = INF;
q.insert({0, 1});
}
void actualizare_nod(int nod, long double valnoua) {
if (d[nod] != INF)
q.erase({d[nod], nod});
d[nod] = valnoua;
q.insert({d[nod], nod});
}
void vizitare_nod(int x) {
for (auto &nod: G[x]) {
if (d[nod.second] > d[x] + nod.first + MARJA) {
actualizare_nod(nod.second, d[x] + nod.first);
ct[nod.second] = ct[x];
} else if (abs(d[x] + nod.first - d[nod.second]) < MARJA)
ct[nod.second] = (ct[nod.second] + ct[x]) % MOD;
}
}
void dijkstra() {
while (!q.empty()) {
TIP nod = *q.begin();
q.erase(nod);
vizitare_nod(nod.second);
}
}
void afisare() {
for (int i = 2; i <= n; ++i)
g << ct[i] << ' ';
}
int main() {
citire();
initializare();
dijkstra();
afisare();
return 0;
}