Religious People Problem

I have written a code https://ide.codingblocks.com/s/76326 and it is passing the sample test cases.
But it is not passing the test case
6 7 2 1
1 3
3 4
2 4
1 2
2 3
1 5
5 6
which i had read in one of the doubts.
I am getting confused as to what error I am making.
My approach is a brute-force one.
I iterate over all vertices and make a temple there in the beginning (since atleast one temple will be there). Then i calculate the distance of all other vertices from this temple vertex. Then i iterate over other vertices and check if A<cost of building road to nearest temple vertes. And then build a temple there is reqd.
Where am I going wrong ?
Please help

Akshay there is a simple approach
if rate of temple is less than cost the making a temple is better
u found just the different componenet of graph and for each component make 1 temple and rest road
here in above example only there is one component so ans is
2+(1*5)=7

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.