Cod sursa(job #1925946)

Utilizator GoogalAbabei Daniel Googal Data 13 martie 2017 21:04:44
Problema Drumuri minime Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <fstream>
#include <cmath>
#include <queue>
#define MOD 104659
#define INF 2000000000
#define NMAX 1501
#define eps 0.0000000001

using namespace std;

ifstream fin("dmin.in");
ofstream fout("dmin.out");

int n, m, nrd[NMAX];
double h[NMAX][NMAX], cost[NMAX];
bool viz[NMAX];

queue <int> coada;

void citire()
{
    int x, y, t,i,j;
    fin >> n >> m;
    for(i = 1; i <= n; i++)
        for(j = 1; j <= n; j++)
            if(i != j) h[i][j] = INF;
    for(i=1; i<=m; i++)
        fin >> x >> y >> t, h[x][y] = h[y][x] = log(t);
}

void BFS(int node)
{
    int i,x;
    coada.push(node);
    viz[node] = 1;
    for(i = 1; i <= n; ++i) cost[i] = INF;
    cost[1] = 0;
    nrd[1] = 1;
    while(!coada.empty())
    {
        x = coada.front();
        coada.pop();
        viz[x] = 0;
        for(i = 1; i <= n; ++i)
        {
            if(x != i)
                if(h[x][i] != INF && cost[i] > cost[x] + h[x][i] + eps)
                {
                    cost[i] = cost[x] + h[x][i];
                    nrd[i] = nrd[x];
                    if(viz[i] == 0) coada.push(i), viz[i] = 1;
                }
                else if(fabs(cost[i] - cost[x] - h[x][i]) < eps && h[x][i] != INF)
                    nrd[i] = (nrd[i] % MOD + nrd[x] % MOD) % MOD;
        }
    }
}

int main()
{
    int i;
    citire();
    BFS(1);
    for(i = 2; i <= n; i++)
        fout << nrd[i] << ' ';
    return 0;
}