Submission #4025085
Source Code Expand
// Package main provides
//
// File: d.go
// Author: ymiyamoto
//
// Created on Wed Jan 16 16:43:18 2019
//
package main
import (
"container/heap"
"fmt"
"sort"
)
type point struct {
index int
x, y int
}
func (p point) xLess(q point) bool {
if p.x == q.x {
return p.y < q.y
}
return p.x < q.x
}
func (p point) yLess(q point) bool {
if p.y == q.y {
return p.x < q.y
}
return p.y < q.y
}
type xpoints []point
func (xp xpoints) Len() int {
return len(xp)
}
func (xp xpoints) Less(i, j int) bool {
return xp[i].xLess(xp[j])
}
func (xp xpoints) Swap(i, j int) {
xp[i], xp[j] = xp[j], xp[i]
}
type ypoints []point
func (yp ypoints) Len() int {
return len(yp)
}
func (yp ypoints) Less(i, j int) bool {
return yp[i].yLess(yp[j])
}
func (yp ypoints) Swap(i, j int) {
yp[i], yp[j] = yp[j], yp[i]
}
type route struct {
to, weight int
}
type item struct {
r route
index int
}
type pQ []*item
func (pq pQ) Len() int {
return len(pq)
}
func (pq pQ) Less(i, j int) bool {
return pq[i].r.weight < pq[j].r.weight
}
func (pq pQ) Swap(i, j int) {
pq[i].r, pq[j].r = pq[j].r, pq[i].r
pq[i].index, pq[j].index = pq[j].index, pq[i].index
}
func (pq *pQ) Push(p interface{}) {
x := p.(*item)
x.index = len(*pq)
*pq = append(*pq, x)
}
func (pq *pQ) Pop() interface{} {
old := *pq
n := len(old)
i := old[n-1]
*pq = old[0 : n-1]
return i
}
func prim(g [][]route) int {
visited := make([]bool, len(g))
visited[0] = true
pq := make(pQ, len(g[0]))
for i := range g[0] {
r := g[0][i]
pq[i] = &item{r: r}
}
heap.Init(&pq)
ans := 0
for len(pq) > 0 {
p := heap.Pop(&pq).(*item)
if !visited[p.r.to] {
ans += p.r.weight
visited[p.r.to] = true
for i := range g[p.r.to] {
heap.Push(&pq, &item{r: g[p.r.to][i]})
}
}
}
return ans
}
func main() {
var N int
fmt.Scan(&N)
xps := make(xpoints, N)
yps := make(ypoints, N)
for i := range xps {
fmt.Scan(&xps[i].x, &xps[i].y)
xps[i].index = i
yps[i].index, yps[i].x, yps[i].y = xps[i].index, xps[i].x, xps[i].y
}
sort.Sort(xps)
sort.Sort(yps)
g := make([][]route, N)
for i := range xps {
if i+1 < len(xps) {
d := xps[i+1].x - xps[i].x
a, b := xps[i].index, xps[i+1].index
g[a] = append(g[a], route{to: b, weight: d})
g[b] = append(g[b], route{to: a, weight: d})
}
if i+1 < len(yps) {
d := yps[i+1].y - yps[i].y
a, b := yps[i].index, yps[i+1].index
g[a] = append(g[a], route{to: b, weight: d})
g[b] = append(g[b], route{to: a, weight: d})
}
}
fmt.Println(prim(g))
}
Submission Info
Submission Time |
|
Task |
D - Built? |
User |
mohei |
Language |
Go (1.6) |
Score |
500 |
Code Size |
2684 Byte |
Status |
AC |
Exec Time |
1758 ms |
Memory |
30080 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
500 / 500 |
Status |
|
|
Set Name |
Test Cases |
Sample |
s1.txt, s2.txt |
All |
01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, s1.txt, s2.txt |
Case Name |
Status |
Exec Time |
Memory |
01.txt |
AC |
1705 ms |
24832 KB |
02.txt |
AC |
1726 ms |
24832 KB |
03.txt |
AC |
1752 ms |
24832 KB |
04.txt |
AC |
1711 ms |
25472 KB |
05.txt |
AC |
1697 ms |
24960 KB |
06.txt |
AC |
1696 ms |
24960 KB |
07.txt |
AC |
1758 ms |
24960 KB |
08.txt |
AC |
1708 ms |
24960 KB |
09.txt |
AC |
1661 ms |
27264 KB |
10.txt |
AC |
1648 ms |
27264 KB |
11.txt |
AC |
1655 ms |
27392 KB |
12.txt |
AC |
1688 ms |
27264 KB |
13.txt |
AC |
1249 ms |
27392 KB |
14.txt |
AC |
1216 ms |
27648 KB |
15.txt |
AC |
975 ms |
23424 KB |
16.txt |
AC |
1228 ms |
25728 KB |
17.txt |
AC |
1381 ms |
26624 KB |
18.txt |
AC |
1321 ms |
26880 KB |
19.txt |
AC |
1019 ms |
26752 KB |
20.txt |
AC |
1032 ms |
28160 KB |
21.txt |
AC |
1012 ms |
27648 KB |
22.txt |
AC |
1064 ms |
22528 KB |
23.txt |
AC |
500 ms |
30080 KB |
24.txt |
AC |
782 ms |
28544 KB |
25.txt |
AC |
1 ms |
512 KB |
26.txt |
AC |
1 ms |
512 KB |
s1.txt |
AC |
1 ms |
512 KB |
s2.txt |
AC |
1 ms |
512 KB |