Cod sursa(job #24755)

Utilizator megabyteBarsan Paul megabyte Data 3 martie 2007 15:45:06
Problema PScNv Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <cstdio>
#include <cstdlib>
#include <list>
#include <iterator>

#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;
char bit[NMAX]={0};
FILE *in,*out;

struct nod
{
	int c,k;
	nod *next;
};
struct head
{
	nod *p,*q;
}lst[NMAX];
void kmin(int source)
{
	register int nd,aux,i,j;
	nod *op;
        list<int> lt[KMAX];
	//list<int>::iterator it;
	lt[0].push_back(source);j=0;
	for(i=1;i<=n;++i)
	{
		aux=0;
		while(j<KMAX&&!aux)
		{
			if(!lt[j].empty())
			{
				do{
				        nd=*lt[j].begin();
					lt[j].pop_front();
					if(best[nd]==j) aux=1;
				  }while(!aux&&!lt[j].empty());
			        if(!aux) j++;
			}
		        else j++;
		}	
		op=lst[nd].p;
		//printf("%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);
			}
			op=op->next;
		}
	//	hp.debugh();
	}
}

int  main()
{
	int x,y,i,a,b,pd;
	nod *op;
	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;
}