結果

問題 No.90 品物の並び替え
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2017-01-17 18:12:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 581 ms / 5,000 ms
コード長 1,542 bytes
コンパイル時間 2,964 ms
コンパイル使用メモリ 75,896 KB
実行使用メモリ 88,952 KB
最終ジャッジ日時 2023-08-24 16:39:51
合計ジャッジ時間 6,805 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 160 ms
56,704 KB
testcase_01 AC 323 ms
62,452 KB
testcase_02 AC 157 ms
56,808 KB
testcase_03 AC 240 ms
59,948 KB
testcase_04 AC 248 ms
59,720 KB
testcase_05 AC 326 ms
62,388 KB
testcase_06 AC 322 ms
62,660 KB
testcase_07 AC 179 ms
57,040 KB
testcase_08 AC 159 ms
56,928 KB
testcase_09 AC 581 ms
88,952 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        int [][] score = new int [N][N];
        for(int i=0; i<M; i++){
            score[sc.nextInt()][sc.nextInt()]=sc.nextInt();
        }
        int ans=0;
        int P=1;
        for(int i=1; i<=N; i++){
            P*=i;
        }
        String [] perm = new String [P];
        makePerm(0, new int [N], new boolean [N+1], perm);
        for(int i=0; i<P; i++){
            String S = perm[i];
            int tmp=0;
            for(int x=0; x<N; x++){
                int a=S.charAt(x)-'1';
                for(int y=x+1; y<N; y++){
                    int b=S.charAt(y)-'1';
                    tmp+=score[a][b];
                }
            }
            ans=Math.max(ans,tmp);
        }
        System.out.println(ans);
    }
    
    static int k=0;
    static String [] makePerm(int n, int[] perm, boolean[] flag, String[] p) {
        if(n==perm.length){
            p[k]=setPerm(perm);
            k++;
        }else{
            for(int i=1; i<=perm.length; i++){
                if(flag[i])continue;
                perm[n]=i;
                flag[i]=true;
                makePerm(n+1,perm,flag,p);
                flag[i]=false;
            }
        }
        return p;
    }
    static String setPerm(int[] perm) {
        String tmp="";
        for (int i: perm) {
            tmp=tmp+i;
        }
        return tmp;
    }
}
0