結果

問題 No.90 品物の並び替え
ユーザー shinwisteriashinwisteria
提出日時 2017-03-28 18:50:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,085 ms / 5,000 ms
コード長 1,674 bytes
コンパイル時間 3,981 ms
コンパイル使用メモリ 75,932 KB
実行使用メモリ 121,984 KB
最終ジャッジ日時 2023-09-20 18:33:59
合計ジャッジ時間 8,034 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
56,056 KB
testcase_01 AC 380 ms
70,640 KB
testcase_02 AC 129 ms
55,656 KB
testcase_03 AC 210 ms
60,800 KB
testcase_04 AC 213 ms
61,076 KB
testcase_05 AC 375 ms
70,612 KB
testcase_06 AC 367 ms
70,872 KB
testcase_07 AC 160 ms
55,680 KB
testcase_08 AC 128 ms
55,696 KB
testcase_09 AC 1,085 ms
121,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Scanner;
public class Narabikae {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt(),M = s.nextInt();
		int[][] score = new int[N][N];
		for(int i = 0;i < M;i++){
			score[s.nextInt()][s.nextInt()] = s.nextInt();
		}
		s.close();
		ArrayList<Integer> number = new ArrayList<>();
		int maxscore = 0;
		for(int i = 1;i <= N;i++){
			number.add(i);
		}
		ArrayList<ArrayList<Integer>> All = new ArrayList<>();
		for(ArrayList<Integer> b:Permutation(number)){
			All.add(b);
		}
		for(ArrayList<Integer> list:All){
			//System.out.println(list);
		}
		for(ArrayList<Integer> list:All){
			int total = 0;
			for(int i = 0;i < N-1;i++){
				for(int j = i+1;j < N;j++){
					total += score[list.get(i)-1][list.get(j)-1];
				}
			}
			//System.out.println(list + "  " + total);
			maxscore = Integer.max(maxscore, total);
		}
		System.out.println(maxscore);
	}
	static ArrayList<ArrayList<Integer>> Permutation(ArrayList<Integer> list){
		int N = list.size();
		if(N == 1){
			ArrayList<ArrayList<Integer>> p = new ArrayList<>();
			ArrayList<Integer> q = new ArrayList<>();
			q.addAll(list);
			p.add(q);
			return p;
		}
		ArrayList<ArrayList<Integer>> A = new ArrayList<>();
		for(int i = 0;i < N;i++){
			int first = list.get(i);
			ArrayList<Integer> B = new ArrayList<>();
			for(int j = 0;j < N;j++){
				if(j != i){
					B.add(list.get(j));
				}
			}
			//System.out.println(B);
			for(ArrayList<Integer> c:Permutation(B)){
				ArrayList<Integer> d = new ArrayList<>();
				d.add(first);
				d.addAll(c);
				A.add(d);
				//System.out.println(d);
			}
		}
		return A;
	}

}
0