Cod sursa(job #164216)

Utilizator FlorinC1996Florin C FlorinC1996 Data 23 martie 2008 18:14:43
Problema PScNv Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.97 kb
#include <cstdio>   
#include <cstdlib>   
    
  
#define NMAX 262144   
#define KMAX 1024   
#define INF "pscnv.in"   
#define OUF "pscnv.out"   
#define DAD(a) (a/2)   
#define ST(a) (2*a)   
#define DR(a) (2*a+1)   
#define MAX(a,b) ((a>b)?(a):(b))   
using namespace std;   
int best[NMAX],n,m;   
struct nod   
{   
    int c,k;   
    nod *next;   
};   
struct head   
{   
    nod *p,*q;   
   
}lst[NMAX];   
struct intnod   
{   
    int c;   
    intnod *next;   
};   
struct intlist   
{   
    intnod *p,*q;   
    void push_back(int x);   
    void print();   
};   
void intlist::push_back(int x)   
{   
    intnod *id;   
    id=new intnod;   
    id->c=x;id->next=NULL;   
    if(p==NULL) p=q=id;   
    else  
    {   
        q->next=id;   
        q=id;   
    }   
};   
void intlist::print()   
{   
    intnod *op;   
    op=p;   
    while(op) { printf("%d ",op->c);op=op->next; }   
    printf("\n");   
}   
void kmin(int source)   
{   
    register int nd,aux,i,j;   
    nod *op;   
    intnod *po;   
        intlist lt[KMAX];   
    for(i=0;i<KMAX;i++) lt[i].p=lt[i].q=NULL;   
    lt[0].push_back(source);po=lt[0].p;   
    j=0;nd=0;   
    for(i=1;i<=n;++i)   
    {   
        aux=0;   
        while(j<KMAX&&!aux)   
        {   
            //lt[j].print();   
            po=lt[j].p;   
            if(po!=NULL)   
            {   
                do{   
                        nd=po->c;po=po->next;   
                    lt[j].p=po;   
                    if(best[nd]==j) aux=1;   
                  }while(!aux&&po!=NULL);   
                    if(!aux) j++;   
            }   
                else j++;   
        }      
        op=lst[nd].p;   
        //printf("ND:  %d\n",nd);   
        while(op!=NULL)   
        {   
            aux=MAX(best[nd],op->k);   
            if(aux<best[op->c])   
            {   
                best[op->c]=aux;   
                lt[aux].push_back(op->c);   
                //if(aux==j&&po==NULL) po=lt[j].p;   
            }   
            op=op->next;   
        }   
       
    }   
}   
  
int  main()   
{   
    register int x,y,i,a,b,pd;   
    nod *op;   
    FILE *in,*out;   
    in=fopen(INF,"r");   
    out=fopen(OUF,"w");   
    fscanf(in,"%d %d %d %d",&n,&m,&x,&y);   
    for(i=1;i<=n;i++){ lst[i].p=lst[i].q=NULL;best[i]=KMAX;}   
    for(i=1;i<=m;i++)   
    {   
        fscanf(in,"%d %d %d",&a,&b,&pd);   
        if(a!=b)   
                {   
                op=new nod;   
        op->c=b;op->k=pd;op->next=NULL;   
        if(lst[a].p==NULL) lst[a].p=lst[a].q=op;   
        else {lst[a].q->next=op;lst[a].q=op;}   
                }   
        //else if(a==x) best[x]=pd;   
    }   
//        for(i=1;i<=n;i++) best[i]=min;   
    best[x]=0;   
    kmin(x);   
//  for(i=1;i<=n;i++) printf("%d ",best[i]);   
    fprintf(out,"%d",best[y]);   
    fclose(in);fclose(out);   
    return 0;   
}