Cod sursa(job #503566)

Utilizator skullLepadat Mihai-Alexandru skull Data 23 noiembrie 2010 19:11:52
Problema Radiatie Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.72 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define punct pair<int,int>
#define nod first
#define cost second
#define nmax 15005
#define mmax 30005

int n, m, t, maxim;
int X[mmax], Y[mmax], C[mmax], ind[mmax], GR[nmax], viz[nmax];
int A[nmax][nmax];
vector<punct> G[nmax];

struct cmp
{
	bool operator () (const int &a, const int &b) const
	{
		return C[a] < C[b];
	}
};

void citire ()
{
	freopen("radiatie.in","r",stdin);
	scanf("%d %d %d ", &n, &m, &t);
	for (int i = 1; i <= m; ++i)
	{
		scanf("%d %d %d ", &X[i], &Y[i], &C[i]);
		ind[i] = i; GR[i] = i;
	}
}

int grupa (int x)
{
	if (GR[x] == x) return x;
	GR[x] = grupa(GR[x]);
	return GR[x];
}

void unire (int x, int y, int c)
{
	GR[grupa(x)] = grupa(y);
	G[x].push_back( make_pair(y,c));
	G[y].push_back( make_pair(x,c));
}

void kruskal ()
{
	sort(ind+1, ind+m+1, cmp() );
	for (int i = 1; i <= m; ++i)
		if ( grupa( X[ind[i]] ) != grupa( Y[ind[i]] ) )
			unire( X[ind[i]], Y[ind[i]], C[ind[i]]);
}

void aflamaxime ()
{
	int i, min, j;
	unsigned int k;
	int viz[nmax];
	punct x;
	queue<int> Q;
	for (i = 1; i <= n; ++i)
	{
		for (j = 1; j <= n; ++j) viz[j] = 0;
		Q.push(i); viz[i] = 1;
		while (!Q.empty() )
		{
			min = Q.front (); Q.pop ();
			for (k = 0; k < G[min].size (); ++k)
			{
				x = G[min][k];
				if (!viz[x.nod]) { viz[x.nod]=1; Q.push(x.nod); A[i][x.nod] = max(A[i][min],x.cost);}
			}
		}
	}
}

void drumuri ()
{
	freopen("radiatie.out","w",stdout);
	int i, x, y;
	aflamaxime ();
	for (i = 1; i <= t; ++i)
	{
		scanf("%d %d ", &x, &y);
		printf("%d\n", A[x][y]);
	}
}
	
int main ()
{
	citire ();
	kruskal ();
	drumuri ();
	return 0;
}