Технокубок 2021 - Финал
F
Useful Edges
Codeforces
2400
原题
← EG →
翻译中…

You are given a weighted undirected graph on $n$ vertices along with $q$ triples $(u, v, l)$, where in each triple $u$ and $v$ are vertices and $l$ is a positive integer. An edge $e$ is called useful if there is at least one triple $(u, v, l)$ and a path (not necessarily simple) with the following properties:

翻译中…
  • $u$ and $v$ are the endpoints of this path,
  • $e$ is one of the edges of this path,
  • the sum of weights of all edges on this path doesn't exceed $l$.
翻译中…

Please print the number of useful edges in this graph.

翻译中…

Input

翻译中…

The first line contains two integers $n$ and $m$ ($2\leq n\leq 600$, $0\leq m\leq \frac{n(n-1)}2$).

翻译中…

Each of the following $m$ lines contains three integers $u$, $v$ and $w$ ($1\leq u, v\leq n$, $u\neq v$, $1\leq w\leq 10^9$), denoting an edge connecting vertices $u$ and $v$ and having a weight $w$.

翻译中…

The following line contains the only integer $q$ ($1\leq q\leq \frac{n(n-1)}2$) denoting the number of triples.

翻译中…

Each of the following $q$ lines contains three integers $u$, $v$ and $l$ ($1\leq u, v\leq n$, $u\neq v$, $1\leq l\leq 10^9$) denoting a triple $(u, v, l)$.

翻译中…

It's guaranteed that:

翻译中…
  • the graph doesn't contain loops or multiple edges;
  • all pairs $(u, v)$ in the triples are also different.
翻译中…

Output

翻译中…

Print a single integer denoting the number of useful edges in the graph.

翻译中…

Note

翻译中…

In the first example each edge is useful, except the one of weight $5$.

翻译中…

In the second example only edge between $1$ and $2$ is useful, because it belongs to the path $1-2$, and $10 \leq 11$. The edge between $3$ and $4$, on the other hand, is not useful.

翻译中…

In the third example both edges are useful, for there is a path $1-2-3-2$ of length exactly $5$. Please note that the path may pass through a vertex more than once.

翻译中…

样例

Input #1
4 6
1 2 1
2 3 1
3 4 1
1 3 3
2 4 3
1 4 5
1
1 4 4
Output #1
5
InputCopy OutputCopy
Input #2
4 2
1 2 10
3 4 10
6
1 2 11
1 3 11
1 4 11
2 3 11
2 4 11
3 4 9
Output #2
1
InputCopy OutputCopy
Input #3
3 2
1 2 1
2 3 2
1
1 2 5
Output #3
2
InputCopy OutputCopy
题目信息
题号
1482F
时间限制
5 s
内存限制
512 MB
标签
图论
最短路

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