結果
| 問題 | 
                            No.90 品物の並び替え
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2014-12-09 21:29:03 | 
| 言語 | D  (dmd 2.109.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 131 ms / 5,000 ms | 
| コード長 | 963 bytes | 
| コンパイル時間 | 1,612 ms | 
| コンパイル使用メモリ | 132,736 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-06-12 01:47:14 | 
| 合計ジャッジ時間 | 2,209 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 1 | 
| other | AC * 9 | 
ソースコード
//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;
}