Pagini recente » Cod sursa (job #2744442) | Cod sursa (job #1324574) | Cod sursa (job #2397657) | Cod sursa (job #1797694) | Cod sursa (job #2125888)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
//http://www.infoarena.ro/problema/radiatie
ofstream fout("radiatie.out") ;
struct muchie
{
int x ;
int y;
int c ;
};
struct sol
{
int x ;
int y ;
};
int maxm = 0 ;
bool vizitat[15000] ;
sol S[15000] ;
int g[15000] , r[15000] ;
vector<muchie> M ;
vector<muchie> MUC ;
int n , m , k ;
void citire()
{
ifstream fin("radiatie.in") ;
muchie m1 ;
int i ;
fin >> n >> m >> k ;
for ( i = 1 ; i <= m ; i++ )
{
fin >> m1.x ;
fin >> m1.y ;
fin >> m1.c ;
M.push_back(m1) ;
}
for ( i = 1 ; i <= k ; i++ )
fin >> S[i].x >> S[i].y ;
}
void DFS(int nodi , int nodf)
{
//cout << " intrare in dfs " << nodi << " " << nodf << endl ;
vizitat[nodi] = true ;
for ( int idx = 0 ; idx < MUC.size() ; idx++ )
{
if ( nodi == nodf )
{
//cout << " am terminat ciclul " << maxm << endl ;
fout << maxm << '\n' ;
return;
}
if ( !vizitat[MUC[idx].y] && MUC[idx].x == nodi )
{
if ( maxm < MUC[idx].c )
{
maxm = MUC[idx].c ;
//cout << " schimb pt muchia " << MUC[idx].x << " " << MUC[idx].y << " cu val " << MUC[idx].c << endl ;
}
DFS(MUC[idx].y,nodf) ;
}
}
}
bool compara(muchie m1 , muchie m2)
{
return m1.c < m2.c ;
}
int root ( int x )
{
while ( x != g[x] )
x = g[x] ;
return x ;
}
void unite ( int x , int y )
{
if ( r[x] < r[y] )
g[y] = x;
if ( r[x] > r[y] )
g[x] = y ;
if ( r[x] == r[y] )
{
g[y] = x ;
r[x]++ ;
}
}
void solve()
{
int r1 , r2 ;
int i ;
for ( i = 1 ; i <= n ; i++ )
g[i] = i ;
for ( i = 1 ; i <= m ; i++ )
{
//cout << " tabloul g " ;
r1 = root(M[i-1].x) ;
r2 = root(M[i-1].y) ;
//cout << " r1 " << r1 << " r2 " << r2 << endl ;
if ( r1 != r2 )
{
unite(r1,r2) ;
MUC.push_back(M[i-1]) ;
//cout << " am adaugat muchia " << M[i-1].x << " " << M[i-1].y << " cu costul " << M[i-1].c << endl ;
}
}
}
int main()
{
citire() ;
int j , cocos , i ;
sort(M.begin(),M.end(),compara) ;
solve() ;
/* for ( j = 0 ; j < MUC.size() ; j++ )
cout << " am muchie " << MUC[j].x << " la " << MUC[j].y << " cu val " << MUC[j].c << endl ;*/
//cout << " k este " << k << endl ;
for ( i = 1 ; i <= k ; i++ )
{
//cout << " k este " << k << endl ;
maxm = 0 ;
for ( j = 1 ; j <= n ; j++ )
vizitat[j] = false ;
//cout << " urmeaza apel recursiv pt " << S[i].x << " " << S[i].y << endl ;
if ( S[i].x > S[i].y )
DFS(S[i].y , S[i].x) ;
else
DFS(S[i].x , S[i].y ) ;
}
}