Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "github.com/skorobogatov/input"
- "math"
- "sort"
- )
- func col_road(n int) int {
- return ((n - 1) * n / 2)
- }
- func filling_points(n int, points []Point2D, x,y float64) {
- for i := 0; i < n; i++ {
- input.Scanf("%f %f", &x, &y)
- points[i] = Point2D{x, y}
- }
- }
- type Point2D struct {
- X, Y float64
- }
- type Edge struct {
- IndexA, IndexB int
- Weight float64
- }
- func check_distance(n int, edges []Edge,points []Point2D) {
- k := 0
- for i := 0; i < n; i++ {
- for j := i + 1; j < n; j++ {
- edges[k] = Edge{i, j, calcDistance(points[i],points[j])}
- k++
- }
- }
- }
- func GetDistanceALL(point Point2D,another Point2D) (float64,float64) {
- legA := math.Abs(point.X - another.X)
- legB := math.Abs(point.Y - another.Y)
- return float64(math.Pow(legA,2)), float64(math.Pow(legB,2))
- }
- func calcDistance(point Point2D, another Point2D) float64{
- b, a := GetDistanceALL(point ,another)
- return math.Sqrt(a + b)
- }
- func main() {
- var road int
- var Kruskal func (edges []Edge, ids []int) float64
- input.Scanf("%d",&road)
- points := make([]Point2D, road)
- edges := make([]Edge,col_road(road))
- var ids []int
- ids = make([]int, road)
- var x, y float64
- filling_points(road, points, x, y)
- check_distance(road, edges, points)
- for i := range ids {ids[i] = i}
- Kruskal = func(edges []Edge, ids []int) float64 {
- summaryWeight := 0.0
- counter := 0
- for i := 0; counter < len(ids) - 1; i++ {
- indexA, indexB := edges[i].IndexA, edges[i].IndexB
- if ids[indexA] != ids[indexB] {counter++;summaryWeight += edges[i].Weight
- oldId, newId := ids[indexB], ids[indexA]
- for j := 0; j < len(ids); j++ {
- if ids[j] == oldId {ids[j] = newId}
- }
- }
- }
- return summaryWeight
- }
- cmp := func(i, j int) bool {return edges[i].Weight < edges[j].Weight}
- sort.Slice(edges, cmp)
- fmt.Printf("%.2fn", Kruskal(edges, ids))
- }
Add Comment
Please, Sign In to add comment