Cod sursa(job #3296921)

Utilizator claudiu.belciugBelciug Claudiu claudiu.belciug Data 18 mai 2025 19:03:24
Problema Distante Scor 40
Compilator java Status done
Runda Arhiva de probleme Marime 3.43 kb
// SPDX-License-Identifier: BSD-3-Clause

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

public class Main {
    static final long INF = Long.MAX_VALUE / 4;

    public static void main(String[] args) throws IOException {
        FastReader in = new FastReader("distante.in");
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("distante.out")));

        int T = in.nextInt();
        StringBuilder ans = new StringBuilder();
        for (int t = 0; t < T; t++) {
            int N = in.nextInt();
            int M = in.nextInt();
            int S = in.nextInt();

            long[] given = new long[N + 1];
            for (int i = 1; i <= N; i++) {
                given[i] = in.nextLong();
            }

            List<List<Edge>> adj = new ArrayList<>(N + 1);
            for (int i = 0; i <= N; i++) {
                adj.add(new ArrayList<>());
            }
            for (int i = 0; i < M; i++) {
                int u = in.nextInt();
                int v = in.nextInt();
                int c = in.nextInt();
                adj.get(u).add(new Edge(v, c));
                adj.get(v).add(new Edge(u, c));
            }

            long[] dist = dijkstra(N, S, adj);

            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;
                }
            }
            ans.append(ok ? "DA" : "NU");
            if (t < T - 1) ans.append('\n');
        }
        out.print(ans.toString());
        out.close();
    }

    static long[] dijkstra(int N, int source, List<List<Edge>> adj) {
        long[] dist = new long[N + 1];
        Arrays.fill(dist, INF);
        dist[source] = 0;

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

        while (!pq.isEmpty()) {
            Node cur = pq.poll();
            int u = cur.vertex;
            long d = cur.dist;
            if (d != dist[u]) continue;
            for (Edge e : adj.get(u)) {
                int v = e.to;
                long nd = d + e.cost;
                if (nd < dist[v]) {
                    dist[v] = nd;
                    pq.add(new Node(v, nd));
                }
            }
        }
        return dist;
    }

    static class Edge {
        int to;
        int 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;
        }
        public int compareTo(Node other) {
            return Long.compare(this.dist, other.dist);
        }
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
        FastReader(String filename) throws IOException {
            br = new BufferedReader(new FileReader(filename));
        }
        String next() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                String line = br.readLine();
                if (line == null) return null;
                st = new StringTokenizer(line);
            }
            return st.nextToken();
        }
        int nextInt() throws IOException { return Integer.parseInt(next()); }
        long nextLong() throws IOException { return Long.parseLong(next()); }
    }
}