Cod sursa(job #2774858)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 13 septembrie 2021 09:44:54
Problema Drumuri minime Scor 100
Compilator c-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
typedef struct N {
    int i;
    double c;
    struct N *n;
}G;
int n,m,h[1501],p[1501],k,b[1501],x,y,i,r,c;
G *a[1501],*q;
double d[1501],z;
void A(int e,int b,double c)
{
    G *q=(G*)malloc(sizeof(G));
    q->i=b,q->c=c,q->n=a[e],a[e]=q;
}
void U(int b)
{
    int t,c;
    for(;b>1;) {
        t=b>>1;
        if(d[h[t]]-d[h[b]]>1e-10)
            p[h[b]]=t,p[h[t]]=b,c=h[t],h[t]=h[b],h[b]=c,b=t;
        else
            b=1;
    }
}
void D(int b)
{
    int f,c;
    for(;b<=k;) {
        f=b;
        if((b<<1)<=k) {
            f=b<<1;
            if(f<k&&d[h[f+1]]-d[h[f]]<1e-10)
                ++f;
        }
        else
            return;
        if(d[h[b]]-d[h[f]]>1e-10)
            p[h[b]]=f,p[h[f]]=b,c=h[b],h[b]=h[f],h[f]=c,b=f;
        else
            return;
    }
}
int main()
{
    freopen("dmin.in","r",stdin),freopen("dmin.out","w",stdout),scanf("%d%d",&n,&m);
    while(m--)
        scanf("%d%d%lf",&x,&y,&z),A(x,y,log(z)),A(y,x,log(z));
    for(i=2;i<=n;++i)
        d[i]=1<<30,p[i]=-1;
    for(p[1]=h[++k]=b[1]=1,d[1]=1.0;k;) {
        r=h[1],c=h[1],h[1]=h[k],h[k--]=c,p[h[1]]=1,D(1);
        for(q=a[r];q;q=q->n)
            if(d[q->i]-d[r]-q->c>1e-10) {
                d[q->i]=d[r]+q->c,b[q->i]=b[r];
                if(p[q->i]!=-1)
                    U(p[q->i]);
                else
                    h[++k]=q->i,p[h[k]]=k,U(k);
            } else if(fabs(d[q->i]-d[r]-q->c)<=1e-10) {
                b[q->i]+=b[r];
                if(b[q->i]>104659)
                    b[q->i]-=104659;
            }
    }
    for(i=2;i<=n;++i)
        printf("%d ",b[i]);
    return 0;
}