CF1483D.Useful Edges

提高+/省选-

通过率:0%

时间限制:5.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

  • uu and vv are the endpoints of this path,
  • ee is one of the edges of this path,
  • the sum of weights of all edges on this path doesn't exceed ll.

Please print the number of useful edges in this graph.

给你一个包含 nn 个顶点的带权无向图,以及 qq 个三元组 (u,v,l)(u, v, l),其中每个三元组中 uu 和 vv 是顶点,ll 是一个正整数。一条边 ee 被称为有用边,当且仅当存在至少一个三元组 (u,v,l)(u, v, l) 和一条(不一定简单)路径,满足以下条件:

  • 该路径的两个端点为 uu 和 vv,
  • 边 ee 是该路径中的一条边,
  • 该路径上所有边的权重之和不超过 ll。

请输出该图中有用边的数量。

输入格式

The first line contains two integers nn and mm (2≤n≤6002\leq n\leq 600, 0≤m≤n(n−1)20\leq m\leq \frac{n(n-1)}2).

Each of the following mm lines contains three integers uu, vv and ww (1≤u,v≤n1\leq u, v\leq n, u≠vu\neq v, 1≤w≤1091\leq w\leq 10^9), denoting an edge connecting vertices uu and vv and having a weight ww.

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

Each of the following qq lines contains three integers uu, vv and ll (1≤u,v≤n1\leq u, v\leq n, u≠vu\neq v, 1≤l≤1091\leq l\leq 10^9) denoting a triple (u,v,l)(u, v, l).

It's guaranteed that:

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

第一行包含两个整数 nn 和 mm(2≤n≤6002\leq n\leq 600,0≤m≤n(n−1)20\leq m\leq \frac{n(n-1)}2)。

接下来的 mm 行中,每行包含三个整数 uu、vv 和 ww(1≤u,v≤n1\leq u, v\leq n,u≠vu\neq v,1≤w≤1091\leq w\leq 10^9),表示一条连接顶点 uu 和 vv、权重为 ww 的边。

接下来一行仅包含一个整数 qq(1≤q≤n(n−1)21\leq q\leq \frac{n(n-1)}2),表示三元组的数量。

接下来的 qq 行中,每行包含三个整数 uu、vv 和 ll(1≤u,v≤n1\leq u, v\leq n,u≠vu\neq v,1≤l≤1091\leq l\leq 10^9),表示一个三元组 (u,v,l)(u, v, l)。

保证:

  • 图中不含自环或重边;
  • 所有三元组中的 (u,v)(u, v) 对互不相同。

输出格式

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

输出一个整数,表示图中有用的边的数量。

输入输出样例

  • 输入#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

    输出#1

    5
  • 输入#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

    输出#2

    1
  • 输入#3

    3 2
    1 2 1
    2 3 2
    1
    1 2 5

    输出#3

    2

说明/提示

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

In the second example only edge between 11 and 22 is useful, because it belongs to the path 1−21-2, and 10≤1110 \leq 11. The edge between 33 and 44, on the other hand, is not useful.

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

在第一个例子中,除权重为 55 的边外,其余每条边都是有用的。

在第二个例子中,仅有顶点 11 与 22 之间的边是有用的,因为它属于路径 1-21\text{-}2,且满足 10≤1110 \leq 11。而顶点 33 与 44 之间的边则不是有用的。

在第三个例子中,两条边都是有用的,因为存在一条长度恰好为 55 的路径 1-2-3-21\text{-}2\text{-}3\text{-}2。请注意,该路径可以多次经过同一顶点。

输入解题思路,AI测评打分。不知道怎么写?

首页