結果

問題 No.90 品物の並び替え
ユーザー jp_stejp_ste
提出日時 2016-05-26 04:21:30
言語 Java21
(openjdk 21)
結果
AC  
実行時間 937 ms / 5,000 ms
コード長 1,503 bytes
コンパイル時間 2,261 ms
コンパイル使用メモリ 79,636 KB
実行使用メモリ 48,592 KB
最終ジャッジ日時 2024-04-16 16:52:47
合計ジャッジ時間 5,946 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 169 ms
42,648 KB
testcase_01 AC 296 ms
48,204 KB
testcase_02 AC 157 ms
42,572 KB
testcase_03 AC 240 ms
48,360 KB
testcase_04 AC 251 ms
48,592 KB
testcase_05 AC 295 ms
48,584 KB
testcase_06 AC 299 ms
48,268 KB
testcase_07 AC 207 ms
44,684 KB
testcase_08 AC 156 ms
42,556 KB
testcase_09 AC 937 ms
48,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
    static int N, M;
    static int list[][];
    static boolean flag[];
    static int ans = 0;
    
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            N = Integer.parseInt(scan.next());
            M = Integer.parseInt(scan.next());
            flag = new boolean[N];
            list = new int[N][N];
            for(int i=0; i<M; i++) {
                int y = Integer.parseInt(scan.next());
                int x = Integer.parseInt(scan.next());
                int v = Integer.parseInt(scan.next());
                list[y][x] = v;
            }
            pattern("");
            System.out.println(ans);
        }
    }
    
    static void pattern(String now) {
        if(now.length() == N) {
            int sum = 0;
            for(int i=0; i<N; i++) {
                for(int j=0; j<N; j++) {
                    if(list[i][j] > 0) {
                        int left = now.indexOf(""+i);
                        int right = now.indexOf(""+j);
                        if(left < right) {
                            sum += list[i][j];
                        }
                    }
                }
            }
            ans = Math.max(ans, sum);
            return;
        }
        
        for(int i=0; i<N; i++) {
            if(!flag[i]) {
                flag[i] = true;
                pattern(now + i);
                flag[i] = false;
            }
        }
    }
}
0