Cod sursa(job #218876)

Utilizator Data 3 noiembrie 2008 21:10:09
Problema Radiatie Scor 30
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.73 kb
using namespace std;
#include <cstdio>
#include <vector>
#include <algorithm>
#include <functional>
#define pb push_back
#define f first
#define s second
#define sz size
#define II inline
#define FOR(i,a,b)for(int i=a;i<=b;++i)
#define mp make_pair
#define IN "radiatie.in"
#define OUT "radiatie.out"
#define oo 1<<30
#define N_MAX 1<<14
vector< vector< pair<int,int> > > A1(N_MAX),A(N_MAX);int rez,S[N_MAX],SM[N_MAX];bool viz[N_MAX];int N,M,K;II void scan(){ int x,y,cc; freopen(IN,"r",stdin); freopen(OUT,"w",stdout); scanf("%d%d%d",&N,&M,&K); FOR(i,1,M) { scanf("%d%d%d",&x,&y,&cc); A1[x].pb( mp(y,cc)); A1[y].pb( mp(x,cc)); } FOR(i,1,N) sort(A1[i].begin(),A1[i].end());}II int find(int nod,int vecin){ int l =A1[nod].sz()- 1; int step,m;
 for(step =1;step <=l;step <<=1); for(m =0;step;step >>=1) if(m + step <=l && A1[nod][m+step-1].f < vecin) m +=step; if(A1[nod][m].f ==vecin) return A1[nod][m].s; else return oo;}II void make_tree(){ memset(viz,0,sizeof(viz)); viz[1]=true; S[0]=oo; FOR(i,2,N) { S[i]=find(1,i); if(S[i]!=oo) SM[i]=1; } FOR(nr,2,N) { int best(0); FOR(i,1,N) if(!viz[i]&& S[i]< S[best]) best =i; if(!best) break; viz[best]=true;
 A[SM[best]].pb( mp(best,S[best])); A[best ].pb( mp(SM[best],S[best])); FOR(i,1,N) if(!viz[i]&& S[i]> find(best,i)) { S[i]=find(best,i); SM[i]=best; } } vector< vector< pair<int,int> > >().swap(A1);}II void DF(int nod,int key,int rad){ if(nod ==key) { rez =rad; return; } if(rez) return; viz[nod]=true; int l =A[nod].sz()- 1; FOR(i,0,l) if(!viz[A[nod][i].f ]) DF(A[nod][i].f,key, max(rad,A[nod][i].s));
 return;}II void solve(){ int x,y; FOR(i,1,K) { scanf("%d%d",&x,&y); memset(viz,0,sizeof(viz)); viz[x]=true; rez =0; DF(x,y,0); printf("%d\n",rez); }}int main(){ scan(); make_tree(); solve(); return 0;}