Cod sursa(job #3296974)

Utilizator claudiu.belciugBelciug Claudiu claudiu.belciug Data 19 mai 2025 19:35:17
Problema Distante Scor 50
Compilator java Status done
Runda Arhiva de probleme Marime 4.57 kb
// SPDX-License-Identifier: BSD-3-Clause

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

public class Main {
    static class Task {
        public static final String INPUT_FILE = "distante.in";
        public static final String OUTPUT_FILE = "distante.out";

        static final long INF = Long.MAX_VALUE / 4;
        static int N, M, S;
        static ArrayList<Edge>[] adj;

        ArrayList<String> results = new ArrayList<>();

        public void solve() {
            readInput();
            writeOutput();
        }

        /* ============================ Citire ============================ */
        private void readInput() {
            try {
                BufferedReader br = new BufferedReader(new FileReader(INPUT_FILE));
                int T = Integer.parseInt(br.readLine());

                for (int t = 0; t < T; t++) {
                    String[] line = br.readLine().split(" ");
                    N = Integer.parseInt(line[0]);
                    M = Integer.parseInt(line[1]);
                    S = Integer.parseInt(line[2]);

                    long[] given = new long[N + 1];
                    line = br.readLine().split(" ");
                    for (int i = 1; i <= N; i++) {
                        given[i] = Long.parseLong(line[i - 1]);
                    }

                    // build adjacency list
                    adj = new ArrayList[N + 1];
                    for (int i = 1; i <= N; i++) {
                        adj[i] = new ArrayList<>();
                    }

                    for (int i = 0; i < M; i++) {
                        line = br.readLine().split(" ");
                        int u = Integer.parseInt(line[0]);
                        int v = Integer.parseInt(line[1]);
                        int c = Integer.parseInt(line[2]);
                        adj[u].add(new Edge(v, c));
                        adj[v].add(new Edge(u, c));
                    }

                    long[] dist = dijkstra();

                    boolean ok = true;
                    for (int i = 1; i <= N; i++) {
                        long di = dist[i] >= INF / 2 ? -1 : dist[i];
                        if (di != given[i]) {
                            ok = false;
                            break;
                        }
                    }

                    if (ok) {
                        results.add("DA");
                    } else {
                        results.add("NU");
                    }
                }
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        /* ============================ Scriere ============================ */
        private void writeOutput() {
            try {
                BufferedWriter bw = new BufferedWriter(new FileWriter(OUTPUT_FILE));
                for (String line : results) {
                    bw.write(line);
                    bw.newLine();
                }
                bw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        /* ============================ Dijkstra ============================ */

        static long[] dijkstra() {
            long[] dist = new long[N + 1];
            Arrays.fill(dist, INF);
            dist[S] = 0;

            PriorityQueue<Node> pq = new PriorityQueue<>();
            pq.add(new Node(S, 0));

            while (!pq.isEmpty()) {
                Node node = pq.poll();
                int u = node.vertex;
                long d = node.dist;

                if (d != dist[u]) {
                    continue;
                }

                for (Edge edge : adj[u]) {
                    int v = edge.to;
                    long nd = dist[u] + edge.cost;

                    if (nd < dist[v]) {
                        dist[v] = nd;
                        pq.add(new Node(v, nd));
                    }
                }
            }
            return dist;
        }

        static class Edge {
            int to, cost;

            Edge(int to, int cost) {
                this.to = to;
                this.cost = cost;
            }
        }

        static class Node implements Comparable<Node> {
            int vertex;
            long dist;

            Node(int vertex, long dist) {
                this.vertex = vertex;
                this.dist = dist;
            }

            @Override
            public int compareTo(Node other) {
                return Long.compare(this.dist, other.dist);
            }
        }
    }

    public static void main(String[] args) {
        new Task().solve();
    }
}