結果

問題 No.90 品物の並び替え
ユーザー t8m8⛄️t8m8⛄️
提出日時 2015-04-07 10:29:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 765 ms / 5,000 ms
コード長 1,753 bytes
コンパイル時間 3,424 ms
コンパイル使用メモリ 75,240 KB
実行使用メモリ 58,348 KB
最終ジャッジ日時 2023-09-17 06:36:21
合計ジャッジ時間 6,326 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
55,732 KB
testcase_01 AC 205 ms
56,740 KB
testcase_02 AC 124 ms
56,044 KB
testcase_03 AC 170 ms
58,348 KB
testcase_04 AC 165 ms
56,704 KB
testcase_05 AC 225 ms
57,056 KB
testcase_06 AC 204 ms
56,648 KB
testcase_07 AC 136 ms
56,172 KB
testcase_08 AC 124 ms
56,184 KB
testcase_09 AC 765 ms
56,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//No.90 品物の並び替え

import java.util.*;
import java.io.*;
import static java.util.Arrays.*;
import static java.lang.Math.*;

public class No90 {
    
    static final Scanner in = new Scanner(System.in);
    static final PrintWriter out = new PrintWriter(System.out,false);

    static void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        int[] a = new int[n];
        for (int i=0; i<n; i++) a[i] = i;

        int[][] s = new int[m][3];
        for (int i=0; i<m; i++) {
            s[i][0] = in.nextInt();
            s[i][1] = in.nextInt();
            s[i][2] = in.nextInt();
        }

        int max = 0;
        do {
            int score = 0;
            for (int k=0; k<m; k++) {
                for (int i=0; i<n; i++) {
                    if (a[i] == s[k][0]) score += s[k][2];
                    if (a[i] == s[k][1]) break;
                }
            }
            max = max(max,score);
        }while(nextPermutation(a));

        out.println(max);
    }

    static boolean nextPermutation(int[] a) {
        int n = a.length, i, j;
        for (i=n-2; i>=0 && a[i]>=a[i+1]; i--);
        if (i == -1) return false;
        for (j=i+1; j<n && a[i]<a[j]; j++);
        int temp = a[i]; a[i] = a[j-1]; a[j-1] = temp;
        for (int l=i+1, r=n-1; l<r; l++,r--) {
            temp = a[l]; a[l] = a[r]; a[r] = temp;
        }
        return true;
    }

    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        solve();
        out.flush();

        long end = System.currentTimeMillis();
        //trace(end-start + "ms");
        in.close();
        out.close();
    }

    static void trace(Object... o) { System.out.println(deepToString(o));}
}
0