結果

問題 No.90 品物の並び替え
ユーザー seiichiseiichi
提出日時 2021-01-28 15:23:19
言語 Go
(1.22.1)
結果
AC  
実行時間 973 ms / 5,000 ms
コード長 1,510 bytes
コンパイル時間 12,189 ms
コンパイル使用メモリ 207,688 KB
実行使用メモリ 58,456 KB
最終ジャッジ日時 2023-09-08 06:18:01
合計ジャッジ時間 13,424 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 86 ms
10,136 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 9 ms
4,376 KB
testcase_05 AC 86 ms
10,132 KB
testcase_06 AC 86 ms
10,132 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 973 ms
58,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"strconv"
)

func scan() (N int, M int, scoreMap map[string]int) {
	scoreMap = map[string]int{}
	fmt.Scan(&N, &M)
	for i := 0; i < M; i++ {
		var item1, item2 string
		var score int
		fmt.Scan(&item1, &item2, &score)
		key := item1 + "," + item2
		scoreMap[key] = score
	}
	return
}

// func permutaions(n int, arr []int) (res [][]int) {
// 	res = append(res, arr)
// 	return
// }

func generate(n int, arr []int, perms *[][]int) {
	if n == 1 {
		cpy := make([]int, len(arr))
		copy(cpy, arr)
		*perms = append(*perms, cpy)
		return
	} else {
		for i := 0; i < n; i++ {
			generate(n-1, arr, perms)
			if n%2 == 0 {
				arr[i], arr[n-1] = arr[n-1], arr[i]
			} else {
				arr[0], arr[n-1] = arr[n-1], arr[0]
			}
		}
	}
}

func main() {
	N, _, scoreMap := scan()
	// N, _, scoreMap := 4, 9, map[string]int{
	// 	"0,1": 1,
	// 	"0,2": 2,
	// 	"0,3": 3,
	// 	"1,2": 4,
	// 	"1,3": 5,
	// 	"2,3": 6,
	// 	"3,2": 100,
	// 	"2,1": 100,
	// 	"1,0": 100,
	// }
	// N, _, scoreMap := 3, 3, map[string]int{
	// 	"0,2": 6728,
	// 	"2,0": 8444,
	// 	"2,1": 5955,
	// }

	var items []int
	for i := 0; i < N; i++ {
		items = append(items, i)
	}
	perms := [][]int{}
	generate(N, items, &perms)

	var maxScore int

	for _, p := range perms {
		score := 0
		for i := 0; i < len(p); i++ {
			for j := i + 1; j < len(p); j++ {
				comb := strconv.Itoa(p[i]) + "," + strconv.Itoa(p[j])
				score += scoreMap[comb]
			}
		}
		if score > maxScore {
			maxScore = score
		}
	}

	fmt.Print(maxScore)
}
0