結果
問題 | No.90 品物の並び替え |
ユーザー |
![]() |
提出日時 | 2017-01-17 18:12:13 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 608 ms / 5,000 ms |
コード長 | 1,542 bytes |
コンパイル時間 | 2,930 ms |
コンパイル使用メモリ | 79,900 KB |
実行使用メモリ | 77,120 KB |
最終ジャッジ日時 | 2024-12-23 00:48:41 |
合計ジャッジ時間 | 6,058 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 9 |
ソースコード
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;}}