Cod sursa(job #132455)

Utilizator megabyteBarsan Paul megabyte Data 5 februarie 2008 21:05:37
Problema Radiatie Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.96 kb
#include <cstdio>
#include <vector>
#define INF "radiatie.in"
#define OUF "radiatie.out"
#define DAD(arg) (arg)/2
#define ST(arg) 2*(arg)
#define DR(arg) 2*(arg)+1
#define CMP(x,y) (ed[x].l<ed[y].l)
#define sz(arg) arg.size()
#define pb(arg) push_back(arg)
using namespace std;

const int NMAX=16002;
const int MMAX=30002;
const int BIG=2000000000;
short dim,n,m,k,t[NMAX]={0},h[NMAX]={0},niv[NMAX]={0};//lng[i]=lungumieadrumului de la i la t[i]i
int lng[NMAX];
char viz[NMAX]={0};
vector<short> lt[NMAX];//LT=lista de aparitie a nodurilor, a=lista de adiacenta arbvore

struct arc
{
	short x,y;
	int l;
}ed[MMAX];

struct arc2
{
	short nb;
	int l;
};

vector<arc2> a[NMAX];

void lift(short poz)
{
	short i,x;
	i=poz;x=h[i];
	while(i>1&&CMP(x,h[DAD(i)]))
	{
		h[i]=h[DAD(i)];
		i=DAD(i);
	}
	h[i]=x;
}

void sink(short poz)
{
	short best,l,r;
	best=poz;
	l=ST(poz);r=DR(poz);
	if(l<=dim&&CMP(h[l],h[best])) best=l;
	if(r<=dim&&CMP(h[r],h[best])) best=r;
	
	if(best!=poz)
	{
		r=h[poz];h[poz]=h[best];h[best]=r;
		sink(best);
	}
}

void insert(short val)
{
	h[++dim]=val;
	lift(dim);
}

short extract()
{
	if(dim>0)
	{
		short ret;
		ret=h[1];
		h[1]=h[dim];--dim;
		sink(1);
		return ret;
	}
	return 0;
}

void apm(short start)
{
	short i,p,nd,nbx,nby,nr,aux;
	arc2 ab;
	char arb[NMAX]={0};
	arb[start]=1;
	dim=0;
	for(i=0;i<sz(lt[start]);++i)
	{
		p=lt[start][i];
		insert(p);
//		printf("I: %hd %hd %hd\n",ed[p].x,ed[p].y,dim);
	}
	
	nr=n-1;
	while(nr>0)
	{

		p=extract();--nr;
		if(p<=0) while(1);
		nbx=ed[p].x;nby=ed[p].y;
		ab.l=ed[p].l;
		ab.nb=nby;a[nbx].pb(ab);
		ab.nb=nbx;a[nby].pb(ab);//se adauga muchia in arb
		if(arb[nby]) {p=nbx;nbx=nby;nby=p;}
//		printf("E: %hd %hd %hd\n",nbx,nby,dim);

		nd=nby;arb[nd]=1;
		for(i=0;i<sz(lt[nd]);++i)
		{
			p=lt[nd][i];
			nbx=ed[p].x;nby=ed[p].y;
			if(nbx!=nd) { aux=nbx;nbx=nby;nby=aux; }
			if(!arb[nby]) 
			{
				insert(p);
//				printf("I: %hd %hd %hd\n",nbx,nby,dim);
			}
		}
	}
}

void dfs(short nd,short lev)
{
	short i,nb;
	viz[nd]=1;niv[nd]=lev;
	for(i=0;i<sz(a[nd]);++i)
	if(!viz[a[nd][i].nb]) 
	{
		nb=a[nd][i].nb;
		t[nb]=nd;lng[nb]=a[nd][i].l;
		dfs(nb,lev+1);
	}
}

int query(short x,short y)
{
	int max=-1;
	short i,j;
	i=x;j=y;
	if(niv[i]>niv[j])
	{
		while(i>1&&niv[i]>niv[j])
		{
			if(max<lng[i]) max=lng[i];
			i=t[i];
		}
	}
	else
	{
		while(j>1&&niv[j]>niv[i])
		{
			if(max<lng[j]) max=lng[j];
			j=t[j];
		}
	}
	while(i>1&&i!=j)
	{
		if(max<lng[i]) max=lng[i];
		if(max<lng[j]) max=lng[j];
		i=t[i];j=t[j];
	}
	return max;
}

int main()
{
	FILE *in,*out;
	in=fopen(INF,"r");
	out=fopen(OUF,"w");
	short i,alfa,beta;
	int v;
	fscanf(in,"%hd%hd%hd",&n,&m,&k);
	for(i=1;i<=m;++i)
	{
		fscanf(in,"%hd%hd%d",&ed[i].x,&ed[i].y,&ed[i].l);
		lt[ed[i].x].pb(i);
		lt[ed[i].y].pb(i);
	}

	apm(1);
	for(i=0;i<=n;++i) lng[i]=BIG;
	dfs(1,1);
	//for(i=1;i<=n;++i) if(!viz[i]) while(1);
	
	/*for(i=1;i<=k;++i)
	{
		fscanf(in,"%hd%hd",&alfa,&beta);
		v=query(alfa,beta);
		fprintf(out,"%d\n",v);
		if(v<0) while(1);

	}*/

	fclose(in);fclose(out);
	return 0;
}