Pagini recente » Cod sursa (job #2036675) | Cod sursa (job #1841331) | Cod sursa (job #1143005) | Cod sursa (job #858005) | Cod sursa (job #2127366)
#include <bits/stdc++.h>
using namespace std;
ifstream in("radiatie.in");
ofstream out("radiatie.out");
const int nx=15002;
struct muchie
{
int x, y, c;
};
struct graf
{
int d;
int c;
};
bool crit (const muchie a, const muchie b)
{
return a.c<b.c;
}
vector < muchie > w;
vector < graf > v[nx];
vector < int > cost;
bitset < nx > viz;
bool found=false;
int n,m,k,i,j,c;
int t[nx];
int comp (int nod)
{
if(nod==t[nod]) return nod;
t[nod]=comp(t[nod]);
return t[nod];
}
void uneste (int x, int y)
{
t[comp(x)]=comp(y);
}
void dfs (int nod, int u)
{
viz.set(nod);
if(nod==u)
{
int mx=INT_MIN;
for(vector < int > :: iterator it=cost.begin(); it!=cost.end(); it++)
mx=max(mx,*it);
out<<mx<<'\n';
found=true;
return;
}
else
{
for(vector < graf > :: iterator it=v[nod].begin(); it!=v[nod].end(); it++)
if(viz.test(it->d)==false)
{
cost.push_back(it->c);
dfs(it->d,u);
if(found) return;
cost.pop_back();
}
}
}
int main()
{
in>>n>>m>>k;
for(; m; m--)
{
in>>i>>j>>c;
w.push_back({i,j,c});
}
sort(w.begin(),w.end(),crit);
for(i=1; i<=n; i++)
t[i]=i;
int sel=0;
for(vector < muchie > :: iterator it=w.begin(); it!=w.end() && sel<n-1; it++)
if(comp(it->x)!=comp(it->y))
{
uneste(it->x,it->y);
sel++;
v[it->x].push_back({it->y,it->c});
v[it->y].push_back({it->x,it->c});
}
for(;k;k--)
{
in>>i>>j;
found=false;
cost.clear();
viz.reset();
dfs(i,j);
}
return 0;
}