Pagini recente » Cod sursa (job #928637) | Cod sursa (job #1154014) | Cod sursa (job #562144) | Cod sursa (job #2226992) | Cod sursa (job #2204775)
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;
ifstream f("radiatie.in");
ofstream g("radiatie.out");
int t[15001],h[15001],S,a[15001][15001];
vector<bool>viz;
struct muchie
{
int valoare;
int a;
int b;
};
struct muchie2
{
int a,cost;
};
vector<muchie2>la[15001];
void DFS(int x,int P)
{
viz[x]=1;
for(int i=0;i<la[x].size();i++)
if(viz[la[x][i].a]==0)
{
a[P][la[x][i].a]=max(a[P][x],la[x][i].cost);
DFS(la[x][i].a,P);
}
}
int cmp(const void *a,const void *b)
{
struct muchie *ca=(struct muchie *)a;
struct muchie *cb=(struct muchie *)b;
return ca->valoare-cb->valoare;
}
int tata(int x)
{
while(t[x]!=x)
x=t[x];
return x;
}
int main()
{
int n,m,l;
f>>n>>m>>l;
struct muchie v[m+1];
for(int i=1;i<=m;i++)
f>>v[i].a>>v[i].b>>v[i].valoare;
qsort(v+1,m,sizeof(struct muchie),cmp);
for(int i=1;i<=n;i++)
{t[i]=i;
h[i]=0;
}
int k=0;
for(int i=1;i<=m;i++)
{
if(k==n-1)
break;
else
{ int x=tata(v[i].a);
int y=tata(v[i].b);
if(x!=y)
{
k++;
if(h[x]>h[y])
t[y]=x;
else if(h[x]==h[y])
{
h[x]++;
t[y]=x;
}
else
t[x]=y;
struct muchie2 c1,c2;
c2.a=v[i].a;
c1.a=v[i].b;
c1.cost=v[i].valoare;
c2.cost=v[i].valoare;
la[c2.a].push_back(c1);
la[c1.a].push_back(c2);
}
}
}
for(int i=1;i<=l;i++)
{
int x,y;
f>>x>>y;
if(a[x][y]==0&&a[y][x]==0)
{ viz.resize(n,0);
DFS(x,x);
viz.clear();
}
g<<max(a[x][y],a[y][x])<<'\n';
}
return 0;
}