結果

問題 No.357 品物の並び替え (Middle)
ユーザー 37zigen
提出日時 2016-04-02 16:15:59
言語 Java
(openjdk 23)
結果
AC  
実行時間 195 ms / 5,000 ms
コード長 937 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 77,532 KB
実行使用メモリ 41,840 KB
最終ジャッジ日時 2024-10-02 12:28:47
合計ジャッジ時間 5,769 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

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