Cod sursa(job #567144)

Utilizator darrenRares Buhai darren Data 29 martie 2011 19:27:52
Problema Drumuri minime Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <cmath>
#include <fstream>
#include <set> // heap with lazy deletion
#include <vector>

using namespace std;

const double eps = 0.0000001;
const double INF = 1000;
const int MOD = 104659;

typedef pair<double, int> E;
typedef vector<E> VE;

int N, M;
VE V[1502];
set<E> H;
double D[1502];
int nD[1502];

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

    fin >> N >> M;

    int p1, p2; double cost;
    for (int i = 1; i <= M; ++i)
    {
        fin >> p1 >> p2 >> cost;

        V[p1].push_back(make_pair(log10(cost), p2));
        V[p2].push_back(make_pair(log10(cost), p1));
    }

    for (int i = 2; i <= N; ++i) D[i] = INF;
    for (VE::iterator it = V[1].begin(); it != V[1].end(); ++it)
    {
        H.insert(*it);
        nD[it->second] = 1;
        D[it->second] = it->first;
    }

    for (int step = 1; step < N; ++step)
    {
        E now = *H.begin(); H.erase(H.begin());
        for (VE::iterator it = V[now.second].begin(); it != V[now.second].end(); ++it)
            if (fabs(D[now.second] + it->first - D[it->second]) < eps)
                nD[it->second] += nD[now.second];
            else if (D[now.second] + it->first < D[it->second])
            {
                D[it->second] = D[now.second] + it->first;
                nD[it->second] = nD[now.second];
                H.insert(make_pair(D[it->second], it->second));
            }
    }

    for (int i = 2; i <= N; ++i)
        fout << nD[i] << ' ';

    fin.close();
    fout.close();
}