結果

問題 No.90 品物の並び替え
ユーザー packet0packet0
提出日時 2014-12-09 21:29:03
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 135 ms / 5,000 ms
コード長 963 bytes
コンパイル時間 1,291 ms
コンパイル使用メモリ 133,612 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-09-02 19:22:34
合計ジャッジ時間 2,099 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 17 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 3 ms
4,368 KB
testcase_04 AC 3 ms
4,372 KB
testcase_05 AC 17 ms
4,368 KB
testcase_06 AC 17 ms
4,368 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 1 ms
4,372 KB
testcase_09 AC 135 ms
4,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.90 品物の並び替え
//No.98 品物の並び替え (Extra)

import std.stdio;

int[50][50] mat;
int N, M;

void main() {
	readf("%d %d\n", &N, &M);
	for(int i=0; i<M; i++) {
		int item1, item2, score;
		readf("%d %d %d\n", &item1, &item2, &score);
		mat[item1][item2] = score;
	}

	bool[] items;
	items.length = N;
	writeln(getScore(items, N));
}

ulong getScore(const bool[] used, const int remain) {
	ulong max = 0;
	if(!remain) {
		return 0;
	}

	for(int i=0; i<N; i++) {
		if(!used[i]) {
			bool[] tmp = used.dup;
			tmp[i] = true;
			ulong localMax = getScore(tmp, remain-1) + addScore(used, i, remain);
			if(localMax > max)
				max = localMax;
		}
	}
	return max;
}

ulong addScore(const bool[] used, const int item, const int remain) {
	ulong total = 0;
	int count = 0;
	for(int i=0; i<N && count<=remain; i++) {
		if(used[i])
			total += mat[i][item];
		else
			count++;
	}
	return total;
}

0