結果
| 問題 |
No.90 品物の並び替え
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-02-07 19:42:10 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 218 ms / 5,000 ms |
| コード長 | 1,431 bytes |
| コンパイル時間 | 2,496 ms |
| コンパイル使用メモリ | 77,884 KB |
| 実行使用メモリ | 41,616 KB |
| 最終ジャッジ日時 | 2024-12-24 19:54:15 |
| 合計ジャッジ時間 | 5,238 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 9 |
ソースコード
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static int N, M;
static int[] item1, item2, score;
static int[][] mx;
static long ans = 0;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
item1 = new int[M];
item2 = new int[M];
score = new int[M];
mx = new int[N][N];
for (int i = 0; i < M; ++i) {
item1[i] = sc.nextInt();
item2[i] = sc.nextInt();
score[i] = sc.nextInt();
mx[item1[i]][item2[i]] = score[i];
}
int[] ord = new int[N];
for (int i = 0; i < N; ++i)
ord[i] = i;
do {
long sum = 0;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
sum += mx[ord[i]][ord[j]];
}
}
ans = Math.max(ans, sum);
} while (nextPermutation(ord));
System.out.println(ans);
}
static boolean nextPermutation(int[] ord) {
int i = ord.length - 1;
while (i - 1 >= 0 && ord[i - 1] > ord[i])
--i;
if (i == 0)
return false;
int j = i;
while (j + 1 < ord.length && ord[j + 1] > ord[i - 1])
++j;
int d = ord[i - 1];
ord[i - 1] = ord[j];
ord[j] = d;
int s = i;
int t = ord.length - 1;
while (s < t) {
d = ord[s];
ord[s] = ord[t];
ord[t] = d;
++s;
--t;
}
return true;
}
static void tr(Object... objects) {
System.out.println(Arrays.deepToString(objects));
}
}