結果

問題 No.90 品物の並び替え
ユーザー ぴろずぴろず
提出日時 2014-12-07 19:11:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 199 ms / 5,000 ms
コード長 1,090 bytes
コンパイル時間 2,391 ms
コンパイル使用メモリ 74,980 KB
実行使用メモリ 56,552 KB
最終ジャッジ日時 2023-09-02 09:18:27
合計ジャッジ時間 4,348 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,948 KB
testcase_01 AC 161 ms
54,236 KB
testcase_02 AC 126 ms
55,724 KB
testcase_03 AC 161 ms
55,740 KB
testcase_04 AC 166 ms
56,152 KB
testcase_05 AC 180 ms
56,404 KB
testcase_06 AC 158 ms
56,300 KB
testcase_07 AC 132 ms
55,588 KB
testcase_08 AC 126 ms
55,532 KB
testcase_09 AC 199 ms
56,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no090;

import java.util.Scanner;

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++) {
			int item1 = sc.nextInt();
			int item2 = sc.nextInt();
			int s = sc.nextInt();
			score[item1][item2] = s;
		}
		int[] p = new int[n];
		for(int i=0;i<n;i++) {
			p[i] = i;
		}
		int max = 0;
		do {
			int sum = 0;
			for(int i=0;i<n;i++) {
				for(int j=i+1;j<n;j++) {
					sum += score[p[i]][p[j]];
				}
			}
			max = Math.max(max,sum);
		} while (nextPermutation(p));
		System.out.println(max);
	}

	static boolean nextPermutation(int[] p) {
		for(int a=p.length-2;a>=0;--a) {
			if(p[a]<p[a+1]) {
				for(int b=p.length-1;;--b) {
					if(p[b]>p[a]) {
						int t = p[a];
						p[a] = p[b];
						p[b] = t;
						for(++a, b=p.length-1;a<b;++a,--b) {
							t = p[a];
							p[a] = p[b];
							p[b] = t;
						}
						return true;
					}
				}
			}
		}
		return false;
	}

}
0