Codeforces Round 1008 (Div. 1)
G
RGB Walking
Codeforces
3500
原题
← F→
翻译中…

You are given a connected graph with $n$ vertices and $m$ bidirectional edges with weight not exceeding $x$. The $i$-th edge connects vertices $u_i$ and $v_i$, has weight $w_i$, and is assigned a color $c_i$ ($1 \leq i \leq m$, $1 \leq u_i, v_i \leq n$). The color $c_i$ is either red, green, or blue. It is guaranteed that there is at least one edge of each color.

翻译中…

For a walk where vertices and edges may be repeated, let $s_r, s_g, s_b$ denote the sum of the weights of the red, green, and blue edges that the walk passes through, respectively. If an edge is traversed multiple times, each traversal is counted separately.

翻译中…

Find the minimum value of $\max(s_r, s_g, s_b) - \min(s_r, s_g, s_b)$ over all possible walks from vertex $1$ to vertex $n$.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains three integers $n$, $m$, and $x$ ($4 \leq n \leq 2 \cdot 10^5$, $n-1 \leq m \leq 2 \cdot 10^5$, $1 \leq x \leq 2 \cdot 10^5$) — the number of vertices, the number of edges in the graph, and the upper bound on the weight of the edges, respectively.

翻译中…

The next $m$ lines each contain three integers $u_i, v_i, w_i$ and a letter $c_i$ ($1 \leq u_i, v_i \leq n$, $1 \leq w_i \leq x$), representing a bidirectional edge between vertices $u_i$ and $v_i$ with weight $w_i$ and color $c_i$. The color $c_i$ is either 'r', 'g', or 'b', denoting red, green, and blue, respectively.

翻译中…

It is guaranteed that the graph is connected and contains at least one edge of each color. The graph may also contain multiple edges and self-loops.

翻译中…

Additionally, it is guaranteed that the total sum of all values of $n$, the total sum of all values of $m$, and the total sum of all values of $x$ across all test cases do not exceed $2 \cdot 10^5$ individually.

翻译中…

Output

翻译中…

For each test case, output a single integer — the minimum value of $\max(s_r, s_g, s_b) - \min(s_r, s_g, s_b)$ over all walks from vertex $1$ to vertex $n$.

翻译中…

Note

翻译中…

In the first test case, the optimal path is $1 \to 2 \to 3 \to 4$. The edges used are:

翻译中…
  • $1 \to 2$ (red, weight $2$)
  • $2 \to 3$ (green, weight $3$)
  • $3 \to 4$ (blue, weight $2$)
翻译中…

We have $s_r = 2$, $s_g = 3$, and $s_b = 2$. Thus, the answer is $1$.

翻译中…

In the second test case, one of the optimal paths is $1 \to 1 \to 2 \to 1 \to 2 \to 3 \to 4$. The edges used are:

翻译中…
  • $1 \to 1$ (red, weight $1$)
  • $1 \to 2$ (red, weight $1$)
  • $2 \to 1$ (red, weight $1$)
  • $1 \to 2$ (red, weight $1$)
  • $2 \to 3$ (green, weight $4$)
  • $3 \to 4$ (blue, weight $4$)
翻译中…

We have $s_r = s_g = s_b = 4$. Thus, the answer is $0$.

翻译中…

样例

Input #1
3
4 3 3
1 2 2 r
2 3 3 g
3 4 2 b
4 5 4
1 2 1 r
1 1 1 r
2 1 1 r
2 3 4 g
3 4 4 b
4 6 4
1 2 2 r
1 2 2 r
2 3 3 b
1 3 4 r
1 4 1 g
3 4 4 g
Output #1
1
0
0
InputCopy OutputCopy
题目信息
题号
2077G
时间限制
2.5 s
内存限制
256 MB
标签
位运算
中国剩余定理
DFS 及类似
图论
数论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。