Cod sursa(job #1522144)

Utilizator DobosDobos Paul Dobos Data 11 noiembrie 2015 11:48:33
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include <bits/stdc++.h>

using namespace std;
ifstream fin ("dmin.in");
ofstream fout ("dmin.out");
const int MOD = 104659;
const int MAX = 1505;
const int INF = 1e9;
const double eps = 1e-6;
set < pair <double,int> > T;
vector < pair <double,int> > G[MAX];
struct fac{
int c,a;
double d;
}D[MAX];
inline int mod(double x){
return max(x,-x);
}
inline void solve()
{
    int nod,ok;
    double cost;
    D[1].a = 1;
    D[1].d = 1;
    T.insert({0,1});
    while(!T.empty()){
        cost = (*T.begin()).first;
        nod =  (*T.begin()).second;
        T.erase(*T.begin());
        for(int i = 0; i < G[nod].size(); i++){
            if(D[G[nod][i].second].d < D[nod].d*G[nod][i].first){
                D[G[nod][i].second].d = D[nod].d*G[nod][i].first;
                D[G[nod][i].second].c = nod;
                D[G[nod][i].second].a = D[nod].a;
                T.insert({D[G[nod][i].second].d,G[nod][i].second});
            } else if(mod(D[G[nod][i].second].d - D[nod].d*G[nod][i].first) < eps){
                        if(D[G[nod][i].second].c != nod)
                        D[G[nod][i].second].a += D[nod].a;
            }
        }
    }

}
int main()
{
    int n,m,x,y,c;
    fin >> n >> m;
    for(int i = 1; i <= m; i++){
        fin >> x >> y >> c;
        G[x].push_back({log(c),y});
        G[y].push_back({log(c),x});
    }
    for(int i = 2; i <= n; i++)
        D[i].d = INF;
    solve();
    for(int i = 2; i <= n; i++)
        fout << D[i].a << " ";
    return 0;
}