Cod sursa(job #64435)

Utilizator sealTudose Vlad seal Data 3 iunie 2007 11:56:26
Problema PScNv Scor 90
Compilator c Status done
Runda Arhiva de probleme Marime 1.58 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Nm 250001
#define Mm 500000
#define Km 1001
int *G[Nm],D[Nm],Dm[Nm],n,x0,yy;
int X[Mm],Y[Mm],K[Mm],m;
struct node
{
    int x;
    struct node* next;
} *L[Km];

void read(void)
{
    char S[20],*p;
    int i;

    freopen("pscnv.in","r",stdin);
    scanf("%d%d%d%d\n",&n,&m,&x0,&yy);
    for(i=0;i<m;++i)
    {
	gets(S);
	p=strtok(S," ");
	X[i]=atoi(p);
	p=strtok(NULL," ");
	Y[i]=atoi(p);
	p=strtok(NULL," ");
	K[i]=atoi(p);
	++D[X[i]];
    }
}

void solve(void)
{
    int i,d,x,y,*p;
    struct node *q;

    for(i=1;i<=n;++i)
    {
	G[i]=(int*)malloc((D[i]+1)*sizeof(int));
	G[i][D[i]]=0;
	D[i]=0;
    }

    for(i=0;i<m;++i)
	G[X[i]][D[X[i]]++]=(Y[i]<<10)+K[i];

    q=(struct node*)malloc(sizeof(struct node));
    q->x=x0;
    q->next=L[0];
    L[0]=q;
    Dm[x0]=0;
    for(i=1;i<=n;++i)
	if(i!=x0)
	{
	    q=(struct node*)malloc(sizeof(struct node));
	    q->x=i;
	    q->next=L[Km-1];
	    L[Km-1]=q;
	    Dm[i]=Km;
	}

    for(i=0;;++i)
	while(L[i])
	{
	    x=L[i]->x;
	    if(x==yy)
		return;
	    L[i]=L[i]->next;
	    if(Dm[x]<i)
		continue;

	    for(p=G[x];*p;++p)
	    {
		y=*p>>10;
		d=*p&1023;
		if(Dm[x]>d)
		    d=Dm[x];
		if(d<Dm[y])
		{
		    Dm[y]=d;
		    q=(struct node*)malloc(sizeof(struct node));
		    q->x=y;
		    q->next=L[d];
		    L[d]=q;
		}
	    }
	}
}

void write(void)
{
    freopen("pscnv.out","w",stdout);
    printf("%d\n",Dm[yy]);
}

int main(void)
{
    read();
    solve();
    write();
    return 0;
}