Cod sursa(job #6947)

Utilizator DorinOltean Dorin Dorin Data 21 ianuarie 2007 11:05:35
Problema Radiatie Scor 0
Compilator cpp Status done
Runda preONI 2007, Runda 1, Clasele 11-12 Marime 1.61 kb
# include <fstream>

using namespace std;

# define input "radiatie.in"
# define output "radiatie.out"

# define max 15001

long n,m,i,j,k,s[max],x,y,z,u[max],a[max],b[max];

struct lista
{
	long nod,c;
	lista *urm;
}*g[max];

void dist(long x,long d);
void adauga(long x,long y,long z)
{
	lista *c = new lista;
	c->nod = y;
	c->c = z;
	c->urm = g[x];
	g[x] = c;

	c=new lista;

	c->nod = x;
	c->c = z;
	c->urm = g[y];
	g[y] = c;
}


long imparte(long p,long q)
{
	long st=p,dr=q,x=a[p],y = b[p];
	while(st < dr)
	{
		while(st<dr && a[dr] >= x) dr--;
		a[st] = a[dr];
		b[st] = b[dr];
		while(st<dr && a[dr] <= x) st++;
		a[dr] = a[st];
		b[dr] = b[st];
	}
	a[st] = x;
	b[st] = y;

	return st;
}

void qsort(long p,long q)
{
	m = imparte(p,q);
	if(m-1 > p)qsort(p,m-1);
	if(m+1 < q)qsort(m+1,q);
}


int main ()
{
	ifstream fin ( input ) ;
	ofstream fout ( output ) ;

	fin >> n >> m >> k;

	for(i = 1;i<=m;i++)
	{
		fin >> x >> y >> z;
		adauga(x,y,z);
	}
	for(i = 1;i<=k;i++)
		fin >> a[i] >> b[i];

	qsort(1,k);

	for(i = 1;i<=k;i++)
	{
		memset(s,0,sizeof(s));
		memset(u,0,sizeof(s));

		dist(a[i],0);
		int val =  a[i];
		while(a[i] == val)
		{
			fout << s[b[i]] << "\n";
			i++;
		}
		i--;
	}

	return 0;
}
void dist(long x,long d)
{
	u[x] = 1;
	lista *c;
	c=g[x];
	while(c)
	{
		long val = d;
		if(!u[c->nod])
		{
			if(c->c > d)
				d = c->c;
			if(s[c->nod])
			{
				if(d < s[c->nod])
					s[c->nod] = d;
			}
			else
				s[c->nod] = d;
		}
		if(!u[c->nod])
			dist(c->nod,d);
		c=c->urm;
		d = val;
	}
	u[x] = 0;
}