#K6451. Exact Weight Path in an Undirected Graph

    ID: 31991 Type: Default 1000ms 256MiB

Exact Weight Path in an Undirected Graph

Exact Weight Path in an Undirected Graph

You are given an undirected graph with (N) nodes and (M) edges. Each edge connects two nodes (u) and (v) with a positive integer weight (w). Additionally, you are given two special nodes: a start node (S) and a target node (T), as well as an integer (K). Your task is to determine whether there exists a path from (S) to (T) such that the sum of the weights of the edges along the path is exactly (K). If such a path exists, output "YES"; otherwise, output "NO".

Note: A path may pass through the same node or edge multiple times, and each traversal contributes to the total weight.

inputFormat

The input is read from stdin with the following format:

  • The first line contains three integers \(N\), \(M\), and \(K\) \( (1 \leq N \leq 10^5,\ 1 \leq M \leq 10^5,\ 1 \leq K \leq 10^9)\).
  • Each of the following \(M\) lines contains three integers \(u\), \(v\), and \(w\), representing an undirected edge between nodes \(u\) and \(v\) with weight \(w\).
  • The last line contains two integers \(S\) and \(T\), indicating the start and target nodes, respectively.

outputFormat

Output a single line to stdout containing "YES" if a path from (S) to (T) with total weight exactly (K) exists, or "NO" otherwise.## sample

4 4 7
1 2 3
1 3 4
2 4 4
3 4 3
1 4
YES