結果

問題 No.357 品物の並び替え (Middle)
ユーザー 37zigen37zigen
提出日時 2016-04-02 16:15:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 188 ms / 5,000 ms
コード長 937 bytes
コンパイル時間 1,813 ms
コンパイル使用メモリ 77,668 KB
実行使用メモリ 54,556 KB
最終ジャッジ日時 2024-04-10 10:38:05
合計ジャッジ時間 5,072 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
53,908 KB
testcase_01 AC 114 ms
54,024 KB
testcase_02 AC 117 ms
53,528 KB
testcase_03 AC 127 ms
54,008 KB
testcase_04 AC 126 ms
53,848 KB
testcase_05 AC 118 ms
53,464 KB
testcase_06 AC 126 ms
54,220 KB
testcase_07 AC 104 ms
52,948 KB
testcase_08 AC 132 ms
53,976 KB
testcase_09 AC 138 ms
54,212 KB
testcase_10 AC 152 ms
54,512 KB
testcase_11 AC 141 ms
53,936 KB
testcase_12 AC 188 ms
54,556 KB
testcase_13 AC 161 ms
54,304 KB
testcase_14 AC 146 ms
54,304 KB
testcase_15 AC 115 ms
53,184 KB
testcase_16 AC 145 ms
54,460 KB
testcase_17 AC 156 ms
54,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder357;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
	static int[] item1,item2,score;
	static int n,m;
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		//品物が0,1,2,   ,nと名付けられている
		m=sc.nextInt();
		//条件がm本
		item1=new int[m];
		item2=new int[m];
		score=new int[m];
		for(int i=0;i<m;i++){
			item1[i]=sc.nextInt();
			item2[i]=sc.nextInt();
			score[i]=sc.nextInt();
		}
		int[] dp=new int[1<<n];
		Arrays.fill(dp, 0);
		for(int mask=0;mask<1<<n;mask++){
			for(int i=0;i<n;i++){
				if((mask&1<<i)==0){
					int nextmask=mask|1<<i;
					int getScore=0;
					for(int ii=0;ii<m;ii++){
						if(i==item2[ii]){
							if(((1<<item1[ii])&mask)>0){
								getScore+=score[ii];
							}
						}
					}
					dp[nextmask]=Math.max(dp[nextmask], dp[mask]+getScore);
				}
			}
		}
		System.out.println(dp[(1<<n)-1]);	
	}
}
0