結果

問題 No.3011 品物の並び替え (Extra)
ユーザー rpy3cpprpy3cpp
提出日時 2015-05-04 04:33:10
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 191 ms / 5,000 ms
コード長 1,336 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 11,040 KB
実行使用メモリ 9,076 KB
最終ジャッジ日時 2023-09-19 06:40:30
合計ジャッジ時間 2,274 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
9,068 KB
testcase_01 AC 127 ms
8,948 KB
testcase_02 AC 186 ms
8,924 KB
testcase_03 AC 174 ms
9,076 KB
testcase_04 AC 132 ms
9,024 KB
testcase_05 AC 97 ms
9,008 KB
testcase_06 AC 102 ms
9,000 KB
testcase_07 AC 121 ms
9,012 KB
testcase_08 AC 191 ms
9,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import random


def read_data():
    N, M = map(int, input().split())
    score = [[0] * N for i in range(N)]
    for i in range(M):
        a, b, s = map(int, input().split())
        score[a][b] = s
    return N, M, score


def solve(N, M, score):
    sol = list(range(N))
    total = calc_score(sol, score)
    delta = 10**6
    r = 0.995
    record = total
    best_sol = sol[:]
    while int(delta):
        delta *= r
        a = random.randint(0, N-1)
        b = random.randint(0, N-1)
        if a == b:
            continue
        sol[a], sol[b] = sol[b], sol[a]
        new_total = calc_score(sol, score)
        if new_total + delta < total:
            sol[a], sol[b] = sol[b], sol[a]
        else:
            if new_total > record:
                record = new_total
                best_sol = sol[:]
            total = new_total
    return record, best_sol


def calc_score(sol, score):
    n = len(sol)
    total = 0
    for i in range(n-1):
        score_i = score[sol[i]]
        for j in range(i+1, n):
            total += score_i[sol[j]]
    return total

if __name__ == '__main__':
    N, M, score = read_data()
#    N, M = 50, 50 * 49
#    score = [[random.randint(0, 10000) for j in range(N)] for i in range(N)]
    total, solution = solve(N, M, score)
    print(total)
    print(' '.join(map(str, solution)))
0