Cod sursa(job #379151)

Utilizator DraStiKDragos Oprica DraStiK Data 30 decembrie 2009 19:28:04
Problema Drumuri minime Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.57 kb
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;

#define INF 2000000000.0
#define pb push_back
#define mp make_pair
#define MOD 104659
#define EPS 1e-10
#define sc second
#define fs first
#define DIM 1505

vector <pair <int,double> > lst[DIM];
int nrd[DIM],h[DIM],poz[DIM];
double dst[DIM];
int n,m,l;

void read ()
{
    int i,x,y,z;

    scanf ("%d%d",&n,&m);
    for (i=1; i<=m; ++i)
    {
        scanf ("%d%d%d",&x,&y,&z);
        lst[x].pb (mp (y,log (z)));
        lst[y].pb (mp (x,log (z)));
    }
}

inline void downheap (int nod)
{
    int fiu;

    for ( ; nod<=l; )
    {
        if ((nod<<1)<=l)
        {
            fiu=nod<<1;
            if (fiu+1<=l)
                if (EPS<dst[h[fiu]]-dst[h[fiu+1]])
                    ++fiu;
        }
        else
            return ;
        if (dst[h[nod]]-dst[h[fiu]]>EPS)
        {
            poz[h[nod]]=fiu;
            poz[h[fiu]]=nod;
            swap(h[nod],h[fiu]);
            nod=fiu;
        }
        else
            return ;
    }
}

inline void upheap(int nod)
{
    int tata;

    for ( ; nod>1; )
    {
        tata=nod>>1;
        if (dst[h[tata]]-dst[h[nod]]>EPS)
        {
            poz[h[nod]]=tata;
            poz[h[tata]]=nod;
            swap(h[tata],h[nod]);
            nod=tata;
        }
        else
            return ;
    }
}

double abs (double a)
{
    if (a<EPS)
        return -a;
    return a;
}

void solve ()
{
    int i,min;

    for (i=2; i<=n; ++i)
        dst[i]=INF;
    for (nrd[1]=poz[1]=h[l=1]=1; l; )
    {
        min=h[1];
        h[1]=h[l--];
        poz[h[1]]=1;
        downheap (1);
        for (i=0; i<(int)lst[min].size (); ++i)
            if (EPS<dst[lst[min][i].fs]-(dst[min]+lst[min][i].sc))
            {
                dst[lst[min][i].fs]=dst[min]+lst[min][i].sc;
                nrd[lst[min][i].fs]=nrd[min];
                if (poz[lst[min][i].fs])
                    upheap (poz[lst[min][i].fs]);
                else
                {
                    poz[h[++l]=lst[min][i].fs]=l;
                    upheap (l);
                }
            }
            else if (abs (dst[lst[min][i].fs]-(dst[min]+lst[min][i].sc))<=EPS)
                nrd[lst[min][i].fs]=(nrd[lst[min][i].fs]+nrd[min])%MOD;
    }
}

void print ()
{
    int i;

    for (i=2; i<=n; ++i)
        printf ("%d ",nrd[i]);
}

int main ()
{
    freopen ("dmin.in","r",stdin);
    freopen ("dmin.out","w",stdout);

    read ();
    solve ();
    print ();

    return 0;
}