Cod sursa(job #195212)

Utilizator marinMari n marin Data 16 iunie 2008 23:10:32
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <cstdio>
#include <cstdlib>
#define NMAX 250009
#define KMAX 1024
#define MAX(x,y) ((x>y)?(x):(y))
using namespace std;

int ww[NMAX],n,m;

struct nod{int c,k; nod *urm;};

struct hd  { nod *p,*q; } lst[NMAX];


struct idd{	int c;	idd *urm;};

struct intlist{	idd *p,*q;	void pb(int x);};

void intlist::pb(int x){
  idd *id;
  id=new idd;
  id->c=x;id->urm=NULL;
  if(p==NULL) p=q=id;
  else{
    q->urm=id;
    q=id;
  }
};



void kmin(int source){
  register int nd,xx,i,j;
  nod *y1;
  idd *po;
  intlist x1[KMAX];
  for(i=0;i<KMAX;i++) x1[i].p=x1[i].q=NULL;
  x1[0].pb(source);po=x1[0].p;
  j=0;nd=0;
  for(i=1;i<=n;++i){
    xx=0;
    while(j<KMAX&&!xx){
      po=x1[j].p;
      if(po!=NULL){
	do{
	  nd=po->c;po=po->urm;
	  x1[j].p=po;
	  if(ww[nd]==j) xx=1;
	}while(!xx&&po);
	if(!xx) j++;
      }
      else j++;
    }
    y1=lst[nd].p;
    while(y1){ //y1!=NULL
      xx=MAX(ww[nd],y1->k);

      if(xx<ww[y1->c]){
	ww[y1->c]=xx;
	x1[xx].pb(y1->c);
      }
      y1=y1->urm;
    }
  }
}

int main(){
  register int x,y,i,a,b,pd;
  nod *y1;
  FILE *in=fopen("pscnv.in","r");
  FILE *out=fopen("pscnv.out","w");
  fscanf(in,"%d %d %d %d",&n,&m,&x,&y);
  for(i=1;i<=n;i++){ lst[i].p=lst[i].q=NULL;ww[i]=KMAX;}
    for(i=1;i<=m;i++){
      fscanf(in,"%d %d %d",&a,&b,&pd);
      if(a!=b){
	y1=new nod;
	y1->c=b;y1->k=pd;y1->urm=NULL;
	if(lst[a].p==NULL) lst[a].p=lst[a].q=y1;
	else {
	  lst[a].q->urm=y1;
	  lst[a].q=y1;
	}
      }
    }
  ww[x]=0;
  kmin(x);
  fprintf(out,"%d",ww[y]);
  fclose(in);fclose(out);
  return 0;
}