結果

問題 No.357 品物の並び替え (Middle)
ユーザー tenten
提出日時 2021-11-11 13:28:18
言語 Java
(openjdk 23)
結果
TLE  
実行時間 -
コード長 2,068 bytes
コンパイル時間 2,603 ms
コンパイル使用メモリ 78,604 KB
実行使用メモリ 120,120 KB
最終ジャッジ日時 2024-11-22 18:33:11
合計ジャッジ時間 59,238 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 9 TLE * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static int n;
    static ArrayList<HashMap<Integer, Integer>> list = new ArrayList<>();
    static int max = 0;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        n = sc.nextInt();
        int m = sc.nextInt();
        for (int i = 0; i < n; i++) {
            list.add(new HashMap<>());
        }
        for (int i = 0; i < m; i++) {
            int left = sc.nextInt();
            int right = sc.nextInt();
            int score = sc.nextInt();
            list.get(right).put(left, score);
        }
        setMax(0, 0, new HashSet<>());
        System.out.println(max);
    }
    
    static void setMax(int idx, int score, HashSet<Integer> used) {
        if (idx >= n) {
            max = Math.max(max, score);
            return;
        }
        for (int i = 0; i < n; i++) {
            if (used.contains(i)) {
                continue;
            }
            used.add(i);
            int tmp = 0;
            for (int x : list.get(i).keySet()) {
                if (used.contains(x)) {
                    tmp += list.get(i).get(x);
                }
            }
            setMax(idx + 1, score + tmp, used);
            used.remove(i);
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0