結果

問題 No.1898 Battle and Exchange
ユーザー tenten
提出日時 2022-04-11 14:51:18
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 3,588 bytes
コンパイル時間 2,876 ms
コンパイル使用メモリ 82,784 KB
実行使用メモリ 91,840 KB
最終ジャッジ日時 2024-12-15 10:48:01
合計ジャッジ時間 47,173 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 54 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        Card[] cards = new Card[n];
        for (int i = 0; i < n; i++) {
            cards[i] = new Card(i, sc.nextInt(), sc.nextInt(), sc.nextInt());
        }
        int left = 0;
        int right = Integer.MAX_VALUE / 2;
        PriorityQueue<Card> queue = new PriorityQueue<>();
        while (right - left > 1) {
            int x = (left + right) / 2;
            Card current = new Card(-1, 1, 1, x);
            queue.clear();
            queue.add(cards[0]);
            boolean[] visited = new boolean[n];
            boolean[] added = new boolean[n];
            added[0] = true;
            while (queue.size() > 0 && current.getSum() > queue.peek().getSum() && !visited[n - 1]) {
                Card c = queue.poll();
                if (visited[c.idx]) {
                    continue;
                }
                visited[c.idx] = true;
                current.exchange(c);
                for (int y : graph.get(c.idx)) {
                    if (!added[y]) {
                        queue.add(cards[y]);
                        added[y] = true;
                    }
                }
            }
            if (visited[n - 1]) {
                right = x;
            } else {
                left = x;
            }
        }
        System.out.println("1 1 " + right);
    }
    
    static class Card implements Comparable<Card> {
        int idx;
        int[] values;
        
        public Card(int idx, int a, int b, int c) {
            this.idx = idx;
            values = new int[]{a, b, c};
            sort();
        }
        
        public void sort() {
            Arrays.sort(values);
        }
        
        public int getSum() {
            int sum = 0;
            for (int x : values) {
                sum += x;
            }
            return sum;
        }
        
        public int compareTo(Card another) {
            return getSum() - another.getSum();
        }
        
        public void exchange(Card another) {
            if (values[0] < another.values[1] && values[1] < another.values[2]) {
                values[0] = another.values[1];
                values[1] = another.values[2];
                sort();
            } else if (values[0] < another.values[2]) {
                values[0] = another.values[2];
                sort();
            }
        }
     }
}
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 next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0