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
struct edge
{
int u,v,w ;
};
int pr[150] ;
vector<edge> e ;
bool comp(edge a, edge b)
{
return a.w>b.w ;
}
int find_r(int r)
{
return pr[r]==r ? r: find_r(pr[r]) ;
}
int mst(int n)
{
int x = INT_MAX,cnt =0,i,u,v ;
for(i=0 ; i<n ; i++)
pr[i] =i ;
sort(e.begin(),e.end(),comp) ;
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 ;
x = min(x,e[i].w) ;
cnt++ ;
if(cnt==n-1)
break ;
}
}
return x ;
}
int main()
{
int test,u,v,w ,cnt =1,i,ans,n,m ;
cin>>test ;
while(test--)
{
e.clear() ;
sc("%d %d",&n,&m) ;
for(i=1 ; i<=m ; i++)
{
sc("%d %d %d",&u,&v,&w) ;
edge get ;
get.u =u ;
get.v =v ;
get.w = w ;
e.push_back(get) ;
}
ans = mst(n) ;
pf("Case #%d: %d\n",cnt++,ans) ;
}
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
struct edge
{
int u,v,w ;
};
int pr[150] ;
vector<edge> e ;
bool comp(edge a, edge b)
{
return a.w>b.w ;
}
int find_r(int r)
{
return pr[r]==r ? r: find_r(pr[r]) ;
}
int mst(int n)
{
int x = INT_MAX,cnt =0,i,u,v ;
for(i=0 ; i<n ; i++)
pr[i] =i ;
sort(e.begin(),e.end(),comp) ;
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 ;
x = min(x,e[i].w) ;
cnt++ ;
if(cnt==n-1)
break ;
}
}
return x ;
}
int main()
{
int test,u,v,w ,cnt =1,i,ans,n,m ;
cin>>test ;
while(test--)
{
e.clear() ;
sc("%d %d",&n,&m) ;
for(i=1 ; i<=m ; i++)
{
sc("%d %d %d",&u,&v,&w) ;
edge get ;
get.u =u ;
get.v =v ;
get.w = w ;
e.push_back(get) ;
}
ans = mst(n) ;
pf("Case #%d: %d\n",cnt++,ans) ;
}
return 0;
}
No comments:
Post a Comment