Click here to view the problem
/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
#define maxi 1000050
struct edge
{
int u,v,w ;
bool operator <(const edge& p) const
{
return w <p.w ;
}
};
vector<edge> e ;
int pr[maxi] ;
int find_r(int r)
{
return (pr[r]==r) ? r : find_r(pr[r]) ;
}
int mst(int n)
{
sort(e.begin(),e.end()) ;
int i ;
for(i=1 ; i<=n ; i++)
pr[i] =i ;
int cnt =0 ,s =0,u,v ;
for(i=0 ; i<e.size(); i++)
{
u = find_r(e[i].u) ;
v = find_r(e[i].v) ;
if(u!=v)
{
pr[u] =v ;
cnt++ ;
s+=e[i].w ;
if(cnt==n-1)
break ;
}
}
return s ;
}
int main()
{
int n,k,total ;
int u,v,w, first_cost,i, secnd_cost,t =0 ;
while(sc("%d",&n)==1)
{
e.clear() ;
total =0 ;
for(i=1 ; i<n ; i++)
{
cin>>u>>v>>w ;
total+=w ;
}
if(t)
pf("\n") ;
t =1 ;
pf("%d\n", total) ;
int a =2 ;
while(a--)
{
sc("%d",&k) ;
for(i=1 ; i<=k ; i++)
{
cin>>u>>v>>w ;
edge get ;
get.u =u ;
get.v =v ;
get.w =w ;
e.pb(get) ;
}
}
secnd_cost = mst(n) ;
pf("%d\n", secnd_cost) ;
}
return 0;
}
/***
Md. Namzul Hasan
Shahjalal University of Science & Technology,Sylhet.
hasan08sust@gmail.com
***/
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef int in ;
typedef unsigned long long ull ;
const double pi = 2*acos(0) ;
#define maxi 40000
#define pf printf
#define sc scanf
#define pb push_back
#define MEM(x,y) (memset((x),(y),sizeof(x)))
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#define MAX(x,y) ((x) > (y) ? (x) : (y))
#define load(array,size) for(int i=0 ; i<size ; i++) cin>>array[i] ;
#define new_line pf("\n")
#define clear_data(array) memset(array,0,sizeof(array))
#define highest_int 2147483647
#define maxi 1000050
struct edge
{
int u,v,w ;
bool operator <(const edge& p) const
{
return w <p.w ;
}
};
vector<edge> e ;
int pr[maxi] ;
int find_r(int r)
{
return (pr[r]==r) ? r : find_r(pr[r]) ;
}
int mst(int n)
{
sort(e.begin(),e.end()) ;
int i ;
for(i=1 ; i<=n ; i++)
pr[i] =i ;
int cnt =0 ,s =0,u,v ;
for(i=0 ; i<e.size(); i++)
{
u = find_r(e[i].u) ;
v = find_r(e[i].v) ;
if(u!=v)
{
pr[u] =v ;
cnt++ ;
s+=e[i].w ;
if(cnt==n-1)
break ;
}
}
return s ;
}
int main()
{
int n,k,total ;
int u,v,w, first_cost,i, secnd_cost,t =0 ;
while(sc("%d",&n)==1)
{
e.clear() ;
total =0 ;
for(i=1 ; i<n ; i++)
{
cin>>u>>v>>w ;
total+=w ;
}
if(t)
pf("\n") ;
t =1 ;
pf("%d\n", total) ;
int a =2 ;
while(a--)
{
sc("%d",&k) ;
for(i=1 ; i<=k ; i++)
{
cin>>u>>v>>w ;
edge get ;
get.u =u ;
get.v =v ;
get.w =w ;
e.pb(get) ;
}
}
secnd_cost = mst(n) ;
pf("%d\n", secnd_cost) ;
}
return 0;
}
No comments:
Post a Comment