Cod sursa(job #1487724)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 17 septembrie 2015 13:03:59
Problema Drumuri minime Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef struct N {
    int i;
    double c;
    N *n;
}G;
int n,m,h[1501],p[1501],k,b[1501],x,y,i,r;
G *a[1501],*q;
double d[1501],z;
void A(int e,int b,double c) {
    G *q=new G;
    q->i=b,q->c=c,q->n=a[e],a[e]=q;
}
void U(int b) {
    for(int t;b>1;) {
        t=b>>1;
        if(d[h[t]]-d[h[b]]>1e-10)
            p[h[b]]=t,p[h[t]]=b,swap(t,b),b=t;
        else
            b=1;
    }
}
void D(int b) {
    for(int f;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,swap(b,f),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],swap(1,k),k--,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]);
}