Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.61 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "bufio"
  5.     "fmt"
  6.     "log"
  7.     "math/rand"
  8.     "os"
  9.     "sync"
  10.     "time"
  11. )
  12.  
  13. type edge struct {
  14.     u, v int
  15.     p    float32
  16. }
  17.  
  18. func (e *edge) String() string {
  19.     return fmt.Sprintf("%d %d %.1f\n", e.u+1, e.v+1, e.p)
  20. }
  21.  
  22. type testcase struct {
  23.     n, e  int
  24.     edges []edge
  25. }
  26.  
  27. func (t *testcase) String() string {
  28.     ret := ""
  29.     ret += fmt.Sprintf("%d %d\n", t.n, t.e)
  30.  
  31.     for i := 0; i < t.e; i++ {
  32.         ret += t.edges[i].String()
  33.     }
  34.  
  35.     return ret
  36. }
  37.  
  38. func generate(i int, wg *sync.WaitGroup) {
  39.     defer wg.Done()
  40.     s1 := rand.NewSource(int64(i) + time.Now().UnixNano())
  41.     r1 := rand.New(s1)
  42.     t := r1.Intn(10) + 1
  43.     cases := make([]testcase, t)
  44.  
  45.     for ca := 0; ca < t; ca++ {
  46.         n := r1.Intn(49) + 2
  47.         if ca < 7 {
  48.             n = r1.Intn(9) + 2
  49.         }
  50.         e := n - 1
  51.         if n > 2 {
  52.             e += r1.Intn(((n * (n - 1)) / 2) - n + 2)
  53.  
  54.         }
  55.         edges := make([]edge, e)
  56.  
  57.         edgesCnt := 0
  58.  
  59.         perm := make([]int, n)
  60.  
  61.         marked := make(map[int]int)
  62.  
  63.         mask := make(map[int]int)
  64.  
  65.         for i := 0; i < n; i++ {
  66.             mask[i] = int(1 << uint(i))
  67.         }
  68.  
  69.         for j := 0; j < n; j++ {
  70.             perm[j] = j
  71.         }
  72.  
  73.         for j := 0; j < n; j++ {
  74.             id := r1.Intn(n-j) + j
  75.             u := perm[id]
  76.  
  77.             perm[j], perm[id] = perm[id], perm[j]
  78.  
  79.             marked[j] = u
  80.  
  81.             if j == 0 {
  82.                 continue
  83.             }
  84.  
  85.             id2 := r1.Intn(j)
  86.             v := marked[id2]
  87.  
  88.             p := float32(r1.Intn(10)+1) / 10
  89.  
  90.             mask[u] |= (1 << uint(v))
  91.             mask[v] |= (1 << uint(u))
  92.  
  93.             edges[edgesCnt] = edge{
  94.                 u: u,
  95.                 v: v,
  96.                 p: p,
  97.             }
  98.  
  99.             edgesCnt++
  100.         }
  101.  
  102.         extraEdges := e - n + 1
  103.  
  104.         fullmask := int(1<<uint(n)) - 1
  105.  
  106.         for j := 0; j < extraEdges; j++ {
  107.             u := r1.Intn(n)
  108.             for mask[u] == fullmask {
  109.                 u = (u + 1) % n
  110.             }
  111.             umask := mask[u]
  112.  
  113.             v := r1.Intn(n)
  114.  
  115.             for umask&(1<<uint(v)) > 0 {
  116.                 v = (v + 1) % n
  117.             }
  118.  
  119.             p := float32(r1.Intn(10)+1) / 10
  120.  
  121.             mask[u] |= (1 << uint(v))
  122.             mask[v] |= (1 << uint(u))
  123.  
  124.             edges[edgesCnt] = edge{
  125.                 u: u,
  126.                 v: v,
  127.                 p: p,
  128.             }
  129.  
  130.             edgesCnt++
  131.         }
  132.  
  133.         cases[ca] = testcase{
  134.             n:     n,
  135.             e:     e,
  136.             edges: edges,
  137.         }
  138.  
  139.     }
  140.  
  141.     filePath := fmt.Sprintf("data/input%02d.txt", i)
  142.     fmt.Println("filePath:", filePath)
  143.  
  144.     ret := fmt.Sprintf("%d\n", t)
  145.     for ca := 0; ca < t; ca++ {
  146.         ret += cases[ca].String()
  147.     }
  148.  
  149.     f, err := os.Create(filePath)
  150.     if err != nil {
  151.         log.Println(err)
  152.         return
  153.     }
  154.  
  155.     defer f.Close()
  156.  
  157.     w := bufio.NewWriter(f)
  158.     _, err = w.WriteString(ret)
  159.     if err != nil {
  160.         log.Println(err)
  161.         return
  162.     }
  163.  
  164.     w.Flush()
  165. }
  166.  
  167. func genDataSet() {
  168.     wg := sync.WaitGroup{}
  169.     for i := 2; i <= 10; i++ {
  170.         wg.Add(1)
  171.         go generate(i, &wg)
  172.     }
  173.  
  174.     wg.Wait()
  175. }
  176.  
  177. func main() {
  178.     genDataSet()
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement