We need to count edges that are not part of any shortest path between any pair of different vertices. For each unordered pair (u,v), there may be multiple shortest paths. An edge is "contained in a shortest path" if there exists some pair (u,v) and a shortest path from u to v that includes that edge. If the edge is never used in any shortest path for any pair, it's counted.
N up to 100, M up to 1000. We can compute all-pairs shortest distances (APSP) using Floyd-Warshall O(N^3) = 1e6, fine. Then for each edge (i, j, w), we need to determine if there exists some pair (s, t) such that the edge lies on some shortest path from s to t. That is, there exist s and t such that dist[s][i] + w + dist[j][t] == dist[s][t] OR dist[s][j] + w + dist[i][t] == dist[s][t]. Since the graph is undirected, the edge can be traversed in either direction.
We need to check existence of s,t. The naive approach: For each edge (u,v,w), loop over all s and t (N^2) and check condition. That would be O(M * N^2) = 1000 * 10000 = 10^7, acceptable. But we can maybe do something smarter.
However note: An edge can be on a shortest path for pair (u,v) even if u and v are not necessarily distinct? "any pair of different vertices". So we consider s != t. We'll ensure s != t.
But check condition: dist[s][u] + w + dist[v][t] == dist[s][t] OR dist[s][v] + w + dist[u][t] == dist[s][t]. Because a shortest path from s to t may go through the edge in direction from u to v or v to u. We must ensure the edge is used exactly once, but it's okay.
We must also consider that there could be multiple shortest paths; edge is counted if it belongs to at least one. So existence of any (s,t) is enough.
Now we must be careful: The shortest path might involve the edge but also there may be equal distance paths that don't use it. That's fine. Condition equality ensures that the edge is on some shortest path if there exists s,t such that equality holds AND there is a path from s to u and v to t that are themselves shortest subpaths (distances are as computed) and they don't reuse vertices? Actually, if dist[s][u] + w + dist[v][t] equals dist[s][t], then there exists some shortest path from s to t that goes from s to u via shortest path, then edge u-v, then from v to t via shortest path. However, we must ensure that the concatenation of those subpaths does not create a cycle or duplicate vertices? In an undirected graph, concatenating two shortest paths that share vertices could create a walk that is not a simple path, but we can always extract a simple path from it; but the condition equality ensures there is a shortest walk that may not be simple. However, since all weights are positive (c_i >= 1), any shortest path will be simple (no cycles because cycles add positive weight). So the existence of such s,u,v,t with equality implies there is a simple shortest path using that edge (provided that the subpaths s-u and v-t are simple and don't intersect? They might intersect, but then we could have a shorter path or another shortest path that avoids the edge, but the edge could still be on a simple shortest path if we can choose non-intersecting subpaths. Actually, consider a triangle: vertices A,B,C with edges AB=1, AC=1, BC=3. For edge BC (weight 3), is there a pair for which BC is on a shortest path? Check: For pair B-C, dist=1+1=2 via A, so shortest path is 2, not 3. So BC not on any shortest path. For pair A-B? shortest distance 1, BC not used. So answer is 1. Our condition: For edge BC, w=3, check if exists s,t: dist[s][B] + 3 + dist[C][t] == dist[s][t]. For s=B,t=C: dist[B][B]=0 +3 + dist[C][C]=0 =3 vs dist[B][C]=2, not equal. For s=A,t=C: dist[A][B]=1 +3 + dist[C][C]=0=4 vs dist[A][C]=1, no. For s=B,t=A: 0+3+1=4 vs 1. So none. So it works.
But there might be cases where equality holds but the subpaths share vertices, possibly causing the combined walk to not be simple, but could there still be a simple shortest path using the edge? For positive weights, if there is a walk that is a shortest path from s to t but not simple, we can remove cycles to get a simple path with weight less or equal? Removing cycles reduces total weight (since weights positive) so resulting path would be strictly shorter unless cycles have zero weight, but weights >=1, so it's strictly shorter, contradicting that the walk was shortest. So any shortest walk must be simple (no repeated vertices). Therefore if equality holds, there must exist a simple shortest path that respects the distances. But we must ensure that we can combine the subpaths to produce a simple path that uses the edge exactly once. However, if the subpaths s-u and v-t intersect (share a common vertex besides possibly u and v?), then the concatenation might have repeated vertices. But we can then pick a different shortest path for subpaths? The condition dist[s][u] + w + dist[v][t] = dist[s][t] implies that the three segments are shortest individually. But there might be multiple shortest paths from s to u; we can choose one that doesn't intersect the other segment except at appropriate endpoints. Since we have an explicit graph, it might be possible that every shortest s-u path intersects every shortest v-t path, but the equality still holds? Consider a graph like a "bowtie" where s and u are on left, v and t on right, and there is a cut vertex. But if intersection occurs, we can possibly reroute to avoid intersection and still get a shortest path? Actually, the condition equality ensures the sum of distances equals the overall distance. However, if any shortest s-u path and any shortest v-t path intersect at some intermediate vertex x (not u or v), then we would have a path s -> ... -> x (part of s-u) and then x -> ... -> v (maybe?) Actually careful: Suppose we have paths s->u and v->t. If they intersect at x, then we could form s->x (via s-u path) then x->v (maybe part of s-u? Actually x is on s-u path, so there is a subpath from x to u; but not necessarily connecting to v). To get from s to t using edge u-v, we go s->...->u, then u-v, then v->...->t. If the s->u path and v->t path intersect at x, but x might be somewhere not necessarily on the segment that can be used to shortcut? For the concatenated walk s->...->u->v->...->t, if there is overlap, you might be able to skip the edge? For a positive-weighted graph, if there is an intersection, then the distance s to t via x might be less than the sum? But we know the sum equals the distance. Let's explore: Suppose there exists a vertex x that lies on some shortest path from s to u and also on some shortest path from v to t. Since the graph is undirected, we could go s->x (distance d_sx), then x->t (distance d_xt). But d_sx <= dist[s][u] - dist[x][u]? Not directly. If we have both s-u and v-t shortest paths and they intersect at x, we can combine s->x (part of s-u) and x->t (part of v-t). The distance s->t via x is at most d_sx + d_xt. But d_sx + d_xt may be less than dist[s][u] + w + dist[v][t]? Possibly, if w is large. But we know the sum equals dist[s][t]. So we would have d_sx + d_xt <= dist[s][t]? Actually d_sx + d_xt might be less than dist[s][t], which would contradict that dist[s][t] is the shortest distance. But we don't know that d_sx + d_xt is less. Let's derive:
Let P be a shortest s-u path that contains x. So dist[s][u] = dist[s][x] + dist[x][u] (since subpaths are shortest). Similarly, let Q be a shortest v-t path that contains x, so dist[v][t] = dist[v][x] + dist[x][t].
But we don't have relation between dist[x][u] and dist[x][v] etc. However, the triangle inequality gives: dist[s][t] <= dist[s][x] + dist[x][t] = (dist[s][u] - dist[x][u]) + dist[x][t]. Similarly, <= dist[s][v] + dist[v][t] etc. But we have equality: dist[s][t] = dist[s][u] + w + dist[v][t].
So we have dist[s][x] + dist[x][t] >= dist[s][t] (since it's a path). So dist[s][x] + dist[x][t] >= dist[s][u] + w + dist[v][t]. That seems unlikely because dist[s][x] is less than or equal to dist[s][u] (since x is on path to u) but plus dist[x][t] may be larger. In any case, we can't guarantee no intersection. However, we can argue that there exists a simple shortest path using the edge if the equality holds. Standard technique: For each edge (u,v), it lies on some shortest path if and only if there exist vertices s,t such that dist[s][u] + w + dist[v][t] == dist[s][t] OR the symmetric. This condition is necessary and sufficient for the edge to be on some shortest path (given the edge can be used in that direction). Usually in problems like "find edges that belong to at least one shortest path" this condition is used. It works because you can construct a path by taking any shortest s-u path and any shortest v-t path, and concatenating them with the edge. However, if those two paths share vertices, the concatenated walk might not be simple, but we can then remove the overlapping part to obtain a simple path that still uses the edge? Possibly not if the overlap includes the edge? Let's examine a potential counterexample.
Suppose we have graph: s -- u (weight 1), u -- v (weight 1), v -- t (weight 1). Also, there is an extra edge from u to t of weight 2. Actually typical. Edge we consider is (u,v) with w=1. For pair s,t, dist[s][t] = 2 (via u-v) because alternative s-u-t? Actually s-u (1), u-t (2) gives 3. So dist[s][t]=2 via s-u-v-t: 1+1+1=3? Wait s-u=1, u-v=1, v-t=1 sum=3. But there might be direct s-t? Not given. Let's make proper: Graph: vertices s, u, v, t. Edges: s-u (1), s-v (3), u-v (1), v-t (1), u-t (3). Then dist[s][t] = s-u (1) + u-v (1) + v-t (1) = 3? Also s-v (3) + v-t (1)=4, s-u (1) + u-t (3)=4. So shortest is 3. Edge (u,v) with w=1: Check s,t: dist[s][u] =1, dist[v][t]=1 => 1+1+1=3 = dist[s][t], so condition holds. s->u path is just direct edge. v->t path direct. Concatenated is s-u-v-t, which is simple. So fine.
Now consider a graph where the shortest s-u path and shortest v-t path necessarily intersect at some vertex other than u and v. Is it possible that the equality holds but any such concatenation yields a walk with repeated vertices? Example: Four vertices: s, x, u, v, t. Edges: s-x (1), x-u (1), u-v (1), v-x (1), x-t (1). Actually careful. Let's try to force that s->u shortest path goes through x, and v->t shortest path also goes through x, but the combined walk s-x-u-v-x-t repeats x. The total distance might still equal the shortest distance s to t? Let's compute.
Vertices: s, a, u, v, b, t? Hmm.
We need: dist[s][u] = d1, dist[v][t] = d2, and dist[s][t] = d1 + w + d2. Also there exists a vertex x such that every shortest s-u path goes through x, and every shortest v-t path goes through x. But perhaps there is also an alternative s-u path that doesn't use x, but we can choose it. However, the condition equality only requires that there exists some shortest s-u path and some shortest v-t path? Actually the condition uses distances, not explicit paths. So if equality holds, there must be at least one shortest s-u path and one shortest v-t path that, when combined with the edge, give a walk whose total length equals dist[s][t]. But if all shortest s-u paths intersect all shortest v-t paths, then any combination might have overlap, but the walk may have repeated vertices. However, the walk length still equals dist[s][t], so it's a shortest walk. But as argued, a shortest walk in a positive graph cannot have cycles; but repeated vertices may create a cycle? For a walk to have repeated vertices, there must be a cycle. If the combined walk has repeated vertices, then there is a segment that goes from the first occurrence to the second occurrence and back? Actually, if you have vertices s -> ... -> u -> v -> ... -> t, and suppose the s->u path and v->t path both go through x. Then the walk might be: s -> ... -> x -> ... -> u -> v -> ... -> x -> ... -> t. This includes x twice. The segment from first x to second x forms a cycle: x -> ... -> u -> v -> ... -> x. Since all edges have positive weight, this cycle has positive weight. Then we could remove that cycle to get a shorter walk from s to t: skip from first x directly to second x? Actually, we have s to first x, then from first x we go to u -> v -> ... -> second x (which is same vertex). The total distance from first x to second x via the cycle is some positive number. Then the walk s -> ... -> first x -> ... -> second x -> ... -> t. If we remove the cycle (i.e., go from s to first x and then directly to second x? Not possible because first x and second x are same vertex; we could just not go the cycle: after reaching first x, instead of going the long way to come back to x, we could stay at x. That would yield a path s -> ... -> x -> ... -> t (with the part from x to t being the suffix from second x to t). That new walk would have distance: dist(s->first x) + dist(second x->t) = dist(s->first x) + dist(x->t). Since the original walk had dist(s->first x) + (dist(first x->u->v->...->second x)) + dist(second x->t) = dist[s][t]. But dist(first x->u->v->...->second x) > 0, so dist(s->first x) + dist(second x->t) < dist[s][t], contradicting that dist[s][t] is the shortest distance. Therefore such a walk cannot be a shortest walk. So any shortest walk must be simple. Hence, if equality holds, there must exist some shortest s-u path and some shortest v-t path that do not intersect except possibly at endpoints? Actually the endpoints: the s-u path ends at u, and v-t path starts at v. They could intersect at some vertex other than u and v only if the intersection vertex can be used to create a cycle as above, which would contradict minimality. Therefore for a shortest walk using the edge, the s-u path and v-t path must be internally vertex-disjoint (except possibly at u and v if they coincide? But u and v are different vertices because it's an edge, so they are distinct. Actually the s-u path ends at u, and v-t path starts at v, and they could intersect at some vertex x that is both on s-u and v-t; that would cause the walk to have x twice: once before reaching u and again after leaving v. That would create a cycle as described, making it non-shortest. So indeed, if equality holds, there exists a shortest path from s to t that uses the edge (u,v) and the subpaths are shortest and must be internally disjoint (except maybe at u and v). But could there be a scenario where equality holds but any pair of shortest s-u and v-t paths intersect, yet there exists a different shortest s-u path that is not the one we pick? The equality condition only requires existence of some paths, not that all intersect. If the distances satisfy the equation, we can always construct such paths that are disjoint? Let's think: Since dist[s][u] + w + dist[v][t] = dist[s][t], then by the optimal substructure property of shortest paths, the concatenation of a shortest s-u path, the edge (u,v), and a shortest v-t path yields a path (walk) of length equal to the shortest distance. As argued, if that walk had a repeated vertex, we could shorten it, contradicting minimality. Therefore, that particular walk cannot have repeated vertices. But is it possible that every shortest s-u path and every shortest v-t path, when concatenated, yields a walk with repeated vertices? The distances condition does not guarantee that we can pick specific paths that avoid repeats unless we carefully choose. However, the equality implies that there exists at least one shortest s-u path and one shortest v-t path such that the concatenation is simple? Actually, it could be that for all choices, the concatenated walk has repeats, but the length is still minimal. But if a walk has repeats, we can shorten it, so it can't be minimal. Therefore at least one combination yields a simple path. So the existence of such s,t with equality implies there exists a simple shortest path that uses the edge.
Thus condition is sufficient.
Hence algorithm: compute APSP (Floyd-Warshall) with distances. Initialize matrix dist[N+1][N+1] with large numbers, set dist[i][i]=0, for each edge set dist[a][b]=dist[b][a]=c. Then Floyd.
Then for each edge (a,b,c), we need to check if there exists s != t such that (dist[s][a] + c + dist[b][t] == dist[s][t]) OR (dist[s][b] + c + dist[a][t] == dist[s][t]). Since graph is undirected, both directions considered.
Note: s and t can be any vertices, including a or b? For s=a, t=b, the condition becomes dist[a][a]=0 + c + dist[b][b]=c == dist[a][b]? Since dist[a][b] is the shortest path, which might be less than c if there is a shorter alternative. So if edge is not part of any shortest path, it will fail for (a,b) if there is a shorter path. But if the edge is itself the only shortest path between its endpoints (i.e., there is no alternative path with length <= c, but since it's undirected and positive, any alternative path must have length >= c; if equal length, then there is another shortest path not using this edge, but the edge still belongs to some shortest path? For pair (a,b), the edge itself is a path of length c. If dist[a][b] == c, then the edge is a shortest path (maybe among others). So condition holds with s=a, t=b (or s=b, t=a) because dist[a][a] + c + dist[b][b] = c = dist[a][b]. So edge will be counted as being in a shortest path. So that's correct.
Thus we can just iterate s from 1..N, t from 1..N, s!=t, and check equality. For each edge, if any (s,t) satisfies, then edge is used. Count edges not used.
Complexity: O(N^3 + M * N^2). With N=100, M=1000 => 100^3 = 1e6, MN^2=100010000=1e7, total ~11e6, fine.
But we can optimize by early breaking. However, it's fine.
Edge Cases: When there are multiple edges with same weight? The graph has no double edges, so at most one edge between any pair.
What about zero-weight edges? c_i >=1, so positive.
We need to ensure using integer distances, up to 1000 * (N-1) ~ 99,000, so within int.
Potential pitfalls: Floyd-Warshall using adjacency matrix of size N+1 but M up to 1000, but N <= 100, so matrix 100x100. Set initial dist to INF large (like 1e9). Then for each edge set both ways.
After Floyd, we have all-pairs shortest distances.
Now, for each edge, we check condition. Since graph is undirected, we can check only one direction? Actually condition OR symmetric covers both; but because for each (s,t) we check both, we can also just check if there exists (s,t) such that min(dist[s][a] + c + dist[b][t], dist[s][b] + c + dist[a][t]) == dist[s][t]. For speed, we can loop s and t and check both.
But careful: The condition can also be satisfied with s = t? No, we only consider pairs of different vertices, as per problem. So exclude s == t.
Also note: The edge itself might be considered for s = a, t = b, which are different, so that's included.
Implementation details:
-
Read N, M.
-
Initialize dist[N][N] with INF.
-
For i in range(N): dist[i][i] = 0.
-
For each edge: read a,b,c; convert to 0-index; set dist[a][b] = dist[b][a] = c; also store edges in list for later.
-
Floyd: for k in range(N): for i in range(N): for j in range(N): if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j]
-
Then for each edge (u,v,w):
used = False
for s in range(N):
if used: break
for t in range(N):
if s == t: continue
if dist[s][u] + w + dist[v][t] == dist[s][t] or dist[s][v] + w + dist[u][t] == dist[s][t]:
used = True
break
if not used: count += 1
Return count.
Check examples:
Example1:
N=3 M=3 edges: (1,2,1), (1,3,1), (2,3,3)
Compute distances:
1-2:1, 1-3:1, 2-3:2 via 1.
Edge (2,3,3): Check if used. For s=2,t=3: dist[2][2]=0 +3 + dist[3][3]=0 =3 !=2. s=1,t=2: dist[1][2]=1, or using edge? dist[1][2] already 1. For condition: dist[1][2]+3+dist[3][?]? No. So not used. Others edges are used. So count=1.
Example2:
3 2 edges: (1,2,1),(2,3,1). Distances: 1-2:1,2-3:1,1-3:2.
Edge (1,2): For s=1,t=2: 0+1+0=1 ==dist[1][2]=1 => used.
Edge (2,3): similarly used. So count=0.
Matches.
We need to confirm correctness for some tricky cases.
Consider a graph where an edge is part of a shortest path for some pair but not for its own endpoints because there is a shorter path. For example, a square: vertices 1-2-3-4-1? Let's make: 1-2 (1), 2-3 (1), 3-4 (1), 4-1 (5). Also edges 1-3 (3) maybe. Consider edge 4-1 weight 5. It might be part of shortest path between 2 and 4? Shortest path 2-3-4 length 2, vs 2-1-4 via weight 5+? Actually 2-1 (1) + 5 =6, so no. So not used. But check with algorithm.
Better consider edge that is not the shortest between its endpoints but lies on some shortest path between other vertices. For example, vertices A, B, C, D. Edges: A-B (10), B-C (10), A-D (1), D-C (1). So shortest path A-C: via D length 2. Edge B-C weight 10: dist[B][C] = 10, but is there any pair where B-C is on a shortest path? Possibly B to D? B to D: B-A (10) + A-D (1) =11, B-C (10)+C-D (1)=11, so both length 11. So B-C is on a shortest path between B and D? Let's see: B->C->D length 11, which equals shortest distance 11. So condition: s=B, t=D: dist[B][C]=10? Actually dist[B][C] = 10 (direct). Check dist[B][C] + w? Wait we need dist[s][u] + w + dist[v][t]? For edge (B,C) with w=10, we can take u=B, v=C. Then dist[s][u] = dist[B][B]=0, w=10, dist[v][t]=dist[C][D]=1 => total 11, dist[B][D]=11, so condition holds. So edge is used. So algorithm would count as used.
Now consider an edge that is not used: maybe an edge that is longer than any alternative for any pair.
Now, what about an edge that is part of a shortest path only when combined with a different orientation? The condition covers both directions.
Edge Cases: When there are multiple shortest paths for a pair, and edge lies on some but not others. Our condition should detect it.
Now, potential issue: Could there be a case where dist[s][u] + w + dist[v][t] == dist[s][t] but the concatenated path is not simple due to overlapping at u or v? Overlap at u? The s-u path ends at u, the v-t path starts at v. They are distinct, so no overlap. But they could share vertices other than u and v. As argued, that would cause a cycle and contradict minimality, so such a combination can't exist if equality holds? Let's examine a case where dist[s][u] + w + dist[v][t] == dist[s][t] but all shortest s-u paths intersect all shortest v-t paths at some internal vertex. Is that possible? Suppose we have graph where s and u are connected through a single bridge vertex x, and also v and t are connected through the same bridge x, and there is a direct edge between u and v? Let's design: vertices: s, x, u, v, t. Edges: s-x (1), x-u (1), u-v (2), v-x (1), x-t (1). So s to u: s-x-u =2 (unique). v to t: v-x-t =2 (unique). s to t via s-x-t =2 (direct). Now edge u-v w=2. Check condition for s,t: dist[s][u]=2, dist[v][t]=2, w=2 => total=6 vs dist[s][t]=2. So fails.
We need dist[s][u]+w+dist[v][t] = dist[s][t]. That would require the sum to be as small as the direct distance, so w must be small relative to alternatives. In the scenario where subpaths intersect, it's likely the sum would be larger because the intersecting vertex provides a shortcut.
Consider graph where s to u = 2 via path s-a-u, and v to t = 2 via path v-b-t, and there is a direct edge a-b of weight 1 that provides a shortcut from s to t. But we want edge u-v to be on some shortest path. For the condition to hold, we need that the sum equals dist[s][t]. If subpaths intersect, there might be a shorter route that bypasses u-v, making dist[s][t] smaller. So equality likely forces that the subpaths are internally disjoint, because otherwise dist[s][t] would be less than or equal to dist[s][x] + dist[x][t] which is less than the sum if there's overlap? Let's formalize:
Suppose there exists a vertex x that lies on some shortest s-u path and also on some shortest v-t path. Then we have:
dist[s][t] <= dist[s][x] + dist[x][t] (by triangle inequality).
Now dist[s][x] <= dist[s][u] (since x is on a shortest s-u path, dist[s][u] = dist[s][x] + dist[x][u] => dist[s][x] = dist[s][u] - dist[x][u] < dist[s][u] unless dist[x][u]=0 (x=u). Similarly, dist[x][t] <= dist[v][t] + dist[x][v]? Actually v to t path includes x? Since x is on a shortest v-t path, we have dist[v][t] = dist[v][x] + dist[x][t] => dist[x][t] = dist[v][t] - dist[v][x] < dist[v][t] unless x=v. So dist[s][x] + dist[x][t] = (dist[s][u] - dist[x][u]) + (dist[v][t] - dist[v][x]) = dist[s][u] + dist[v][t] - (dist[x][u] + dist[v][x]). Since dist[x][u] + dist[v][x] >= dist[v][u] by triangle? Actually v to u via x: dist[v][u] <= dist[v][x] + dist[x][u], so dist[x][u] + dist[v][x] >= dist[v][u] >= 0. So we get dist[s][x] + dist[x][t] <= dist[s][u] + dist[v][t] - dist[v][u]? Not sure.
But importantly, if x is not u or v, then dist[x][u] > 0 and dist[v][x] > 0, so the sum dist[s][x] + dist[x][t] is strictly less than dist[s][u] + dist[v][t] (since we subtract positive numbers). Then we have:
dist[s][t] <= dist[s][x] + dist[x][t] < dist[s][u] + dist[v][t].
If w > 0, then dist[s][u] + w + dist[v][t] > dist[s][u] + dist[v][t] > dist[s][t]. So equality cannot hold. So indeed, if there is any common vertex x (other than possibly u or v) on some shortest s-u and v-t paths, then dist[s][t] < dist[s][u] + w + dist[v][t] for any positive w. Therefore the equality forces that there is no such common vertex. So the concatenation is vertex-disjoint except for endpoints, thus simple.
Thus condition is both necessary and sufficient.
Hence algorithm is correct.
Now we need to implement efficiently.
Edge Cases:
- N up to 100, M up to 1000, but graph connected so M >= N-1.
- Distances: we use integer.
Implementation Steps in Code:
def solve():
import sys
input = sys.stdin.readline
N,M = map(int,input().split())
INF = 10**9
dist = [[INF]*N for _ in range(N)]
for i in range(N):
dist[i][i] = 0
edges = []
for _ in range(M):
a,b,c = map(int,input().split())
a-=1; b-=1
dist[a][b] = c
dist[b][a] = c
edges.append((a,b,c))
# Floyd-Warshall
for k in range(N):
dk = dist[k]
for i in range(N):
di = dist[i]
dik = di[k]
if dik == INF: continue
for j in range(N):
nd = dik + dk[j]
if nd < di[j]:
di[j] = nd
# Alternatively triple loop simple
# But for speed we can do as above.
ans = 0
for (u,v,w) in edges:
used = False
for s in range(N):
if used: break
ds_u = dist[s][u]
ds_v = dist[s][v]
if ds_u == INF or ds_v == INF: continue # but graph is connected so all reachable.
for t in range(N):
if s == t: continue
# Check first orientation
if ds_u + w + dist[v][t] == dist[s][t]:
used = True
break
if ds_v + w + dist[u][t] == dist[s][t]:
used = True
break
if not used:
ans += 1
print(ans)
But we need to be careful about using INF values; but since graph connected, all distances finite.
Now we need to confirm that we include all pairs s != t. Our loops include all s and t including where t equals u or v etc. That's fine.
We can also early exit when condition met.
Potential optimization: Precompute dist and then for each edge, we can check existence by considering that edge is used if for some s, dist[s][u] + w == dist[s][v]? Actually that's a known condition for edges on shortest paths from a source. But we need for any pair. Another approach: For each edge (u,v,w), it is in some shortest path if and only if there exists a vertex s such that dist[s][u] + w == dist[s][v] AND for that s, there exists t such that the edge lies on a path from s to t? But it's simpler to just check all pairs.
Given constraints, O(M*N^2) is okay.
But careful: M can be up to 1000, N=100 => N^2 = 10,000, product = 10,000,000. For each edge we also compute both orientations. That's about 20 million equality checks, each cheap. In Python, 20 million operations might be borderline but likely okay (under 1 second? Might be ~0.2 sec per 10 million? Actually Python can handle ~50 million simple ops per second maybe? Depends. But to be safe, we can do some optimizations: Precompute dist as list of lists. Then for each edge, we can break loops early. Also we can reduce the inner loop by using vectorized checks? Not needed.
Alternatively, we can compute for each edge whether it is used by checking if for any s, dist[s][u] + w == dist[s][v] AND dist[s][v] + w == dist[s][u]? That's about edges that are on some shortest path tree from s? But edge could be on a shortest path between two vertices that are not either endpoint? For edge to be on a shortest path between s and t, it must satisfy either dist[s][u] + w + dist[v][t] == dist[s][t]. This is like edge lies on some shortest path if it is "tight" for some pair. Another viewpoint: In all-pairs shortest path, an edge (u,v) with weight w is part of some shortest path if and only if there exist vertices x,y such that dist[x][u] + w + dist[v][y] == dist[x][y] OR dist[x][v] + w + dist[u][y] == dist[x][y]. That's what we are doing.
We can also reduce the inner loops by noticing that if we fix s and u, we need to find if there exists t such that dist[v][t] == dist[s][t] - (dist[s][u] + w). This is like checking for each s whether the set of distances from v matches something. But not needed.
Given constraints, brute force is acceptable.
Test with a random small case for sanity.
Edge case: N=2, M=1 (since connected). Only one edge. Then obviously it is on the shortest path between the two vertices. So answer should be 0. Check: dist[0][1]=c. For edge (0,1,c): s=0,t=1 -> 0 + c + 0 = c == dist[0][1] -> used. So ans 0.
If M > 1 for N=2, but no double edges, so M can be at most 1 (since only one possible edge). Actually N=2, max edges = 1 (since no self-loops/double edges). So M=1.
N=3, M=3 as example.
Now, let's consider more complex graph to ensure no false negatives.
Suppose a graph where edge is part of multiple shortest paths: we will detect.
Now, what about edges that are on a shortest path only for a pair where s or t equals the edge's endpoint? That is covered.
Now, check for a scenario where edge is not used because for every pair, the equality fails, but there is a pair where the edge lies on a path that is exactly the shortest distance but due to multiple equal-length segments, but maybe the equality holds? Actually if the edge lies on a shortest path, then by definition there exists s,t and a path P from s to t that includes edge, and length(P)=dist[s][t]. Since P is a shortest path, we can decompose it as s->...->u->v->...->t (or reverse). Then the subpaths from s to u and from v to t are shortest (by optimal substructure). So dist[s][u] = length(s->...->u) and dist[v][t] = length(v->...->t). So the condition holds. So necessary.
Thus algorithm correct.
Now, there is a nuance: The problem asks "edges that are not contained in any shortest path between any pair of different vertices." Does an edge that is on a shortest path from a vertex to itself count? Since pair must be different, we ignore s==t. That's fine.
Now confirm: In the condition, we check for s and t distinct. But we also might check s=t? That would give dist[s][u] + w + dist[v][s] == dist[s][s]=0, which cannot happen with positive w, so no issue.
Now, implement.
We need to be mindful of performance. For each edge, we do double loop over s,t. We can break early when found. Possibly we can invert logic: For each edge, if we can find any s,t such that condition holds, we set used.
But we can also compute for each ordered pair (s,t) the set of edges that lie on some shortest path? That might be more expensive.
Given M up to 1000, N=100, worst-case 10 million checks. Each check involves a few array accesses and addition and comparison. That's fine.
But we might also need to consider that M can be as high as 1000, but N=100. In the worst case, the graph could be dense (N(N-1)/2 up to 4950, but M max is min(N(N-1)/2,1000) = 1000, so M is at most 1000. So MN^2 = 100010000 = 10 million. That's okay.
Potential improvement: Use early exit: for each edge, we can loop over s and for each s compute needed value for t quickly? Not needed.
Let's test worst-case time: 10 million loops, each with two checks. Python can handle that in < 0.5 sec maybe. Plus Floyd-Warshall 1 million operations (100^3) which is also fine.
We should use local variables for speed, maybe pre-fetch dist as local variable inside loops? Actually within loops we access dist many times, so using a local alias might help.
Alternatively, we can implement as:
for u,v,w in edges:
used = False
for s in range(N):
d_su = dist[s][u]
d_sv = dist[s][v]
# For each t:
# we need to check if d_su + w + dist[v][t] == dist[s][t] or d_sv + w + dist[u][t] == dist[s][t]
# We can precompute list of distances from v and u? Already in matrix.
# We'll just loop t.
for t in range(N):
if s == t: continue
if d_su + w + dist[v][t] == dist[s][t] or d_sv + w + dist[u][t] == dist[s][t]:
used = True
break
if used:
break
if not used:
ans += 1
We can also consider using any() with generator but that might be slower.
Given that we break early once used, many edges will be used (likely most edges in a typical graph), so we might exit quickly. But worst-case if graph is a tree, all edges are on some shortest path? In a tree, there is a unique path between any pair, so every edge is on some shortest path (since the unique path is the shortest). Actually in a tree, distances are unique and all edges lie on the path between some pairs (specifically, the two components when edge removed). So used = True for all edges. So for a tree, we will check all s,t for each edge? Actually once we find a pair, we break. For each edge, we can quickly find a pair: the endpoints themselves (s=u, t=v) should work because in a tree the only path between u and v is the edge itself, so dist[u][v] = w, so condition holds: dist[u][u]=0 + w + dist[v][v]=w == dist[u][v]. So we will detect in the first s=u, t=v iteration. So we break early after checking some t? Actually loop order: s from 0..N-1, for each s we loop t. So for edge (u,v), when s=u, we loop t, and when t=v, condition holds. So we break after that. So for each edge we only go through O(N) iterations before hitting the endpoints. Actually s=u is at index u, which could be early or late. Worst-case u=99, we go through s=0..98 before hitting s=99. That's about 99*100=9900 checks, not 10000. So about 10k checks per edge. For M=1000, that's 10 million checks. That's okay.
But for unused edges, we might have to check all s,t before concluding not used. Unused edges might be few, but worst-case all edges unused? Is that possible? Possibly if the graph has many edges that are not on any shortest path, e.g., a complete graph with uniform weights? Let's think: Complete graph with all edges weight 1. Then the shortest path between any two vertices is direct edge (dist=1). Any edge is the direct edge between its endpoints, so it is a shortest path for that pair. So all edges used. Another example: Suppose we have a cycle where all edges weight 1. For each edge, there is an alternative path of length N-1? For a cycle of N vertices, each edge weight 1. The distance between its endpoints is 1 (direct) or the other way around which is N-1 >1. So direct edge is shortest. So used. To have an edge not used, there must be a shorter alternative path between its endpoints, and also for any other pair, using that edge would be longer than alternative. Possibly in a dense graph, many edges might be longer than the shortest path between its endpoints, and also not useful for any other pair because any path that includes it could be shortcut. For example, consider a complete graph where all edges have weight 1 except one heavy edge of weight 100. The heavy edge is not the shortest between its endpoints because there is a direct path of weight 1? Actually direct edge is the heavy edge itself? Wait, if we have a complete graph with uniform weight 1, all edges weight 1, then heavy edge would be weight 100. But in a complete graph, there is also a direct edge of weight 1? No, the heavy edge is the only edge between its endpoints? In a complete graph, each pair has exactly one edge, so if we set some edges heavy, then the heavy edge is the only direct connection; but there may be alternative path via other vertices of total weight 2 (since all other edges weight 1). So dist between its endpoints = 2. The heavy edge weight 100 is not a shortest path for its endpoints. Could it be used for some other pair? Consider pair (a,b) where a and b are not the endpoints of the heavy edge? Possibly the heavy edge could be part of a shortest path if all alternative paths are also heavy? Let's analyze: In a complete graph with many edges weight 1, the shortest distance between any two vertices is either 1 if there is a direct edge weight 1, or 2 if not directly connected? Actually in a complete graph, every pair has a direct edge; but if we assign weights arbitrarily, some edges could be heavy. For a pair (u,v) that are not directly connected by a light edge? In a complete graph, every pair is directly connected, so the shortest path is the direct edge weight (which could be heavy if that's the edge). So for a pair (u,v) that is the heavy edge, the direct edge is heavy, but maybe there is a path of two light edges of total weight 2. So the heavy edge is not used for that pair. For a pair (x,y) where both are not the endpoints of the heavy edge, the direct edge weight (maybe light) gives distance maybe 1. Could the heavy edge be on a shortest path between x and y? Possibly if the direct edge is missing? But in complete graph, it's present. However, if the direct edge between x and y is heavy (say weight 100) and there is an alternative two-step path via some vertex z with edges weight 1+1=2, then the heavy direct edge is not used. But could the heavy edge between u and v be used as an intermediate edge? For some x,y, the shortest path might be x-u-v-y, but that would be weight w(x,u) + 100 + w(v,y). If w(x,u) and w(v,y) are 1, total = 102. But the direct edge between x and y might be 1, so not shortest. If all other edges are heavy as well? But then the graph might have many heavy edges. It's possible that the heavy edge is part of the unique shortest path between some pairs if the direct edge is missing? But in a complete graph, all pairs have direct edges, so a shortest path will never be longer than the direct edge. However, if the direct edge is heavy and there is a two-step path that is even heavier? Let's consider a graph where all edges are heavy (say weight 10) except a few light edges? But to have heavy edge used, we need a scenario where for some pair, the shortest path goes through the heavy edge because all alternative paths are even longer. That could happen if the graph is not complete but still dense. For instance, take a "bridge" heavy edge that is the only connection between two dense clusters. Then it would be used. So many edges may be used.
Thus worst-case number of unused edges might be small but not necessarily. However, we can't rely on early break.
Thus we should ensure O(M * N^2) is okay. Let's compute worst-case operations: For each edge, loops: for s in 0..N-1, for t in 0..N-1 (excluding s). That's N*(N-1) ≈ 10,000 iterations per edge. For M=1000, that's 10 million. Each iteration does two checks each involving a few array accesses and addition. That's about maybe 20 million operations. In Python, 20 million is fine (typically ~0.2 seconds). However, with the overhead of loops in Python (nested loops), it might be a bit slower but still within time limits typical for AtCoder (2 sec). We can further optimize: Precompute for each s the arrays dist[s] as list. Then inner loop: for each edge, we compute ds_u and ds_v, then for t loop, we need dist[v][t] and dist[u][t] and dist[s][t]. We can access via lists.
We can also swap loops: For each s, we can precompute something for all edges? But that's extra complexity.
Given constraints, straightforward is acceptable.
But careful: The Floyd-Warshall with triple loops in Python can be a bit slower if done naively (100^3 = 1e6 iterations, each with inner loop 100 -> actually triple nested loops: for k in range(N): for i in range(N): for j in range(N): if dist[i][k] + dist[k][j] < dist[i][j]: dist[i][j] = dist[i][k] + dist[k][j]. That's N^3 = 1,000,000 operations, which is fine. Actually it's 100100100 = 1,000,000, and inside we do addition and comparison. That's negligible.
Thus overall runtime fine.
Now, double-check problem statement: "edges that are not contained in any shortest path between any pair of different vertices." Could an edge be contained in a shortest path for a pair where the path uses the edge multiple times? Typically a path is simple (no repeated vertices) because otherwise it wouldn't be shortest with positive weights. So we consider simple paths. So condition holds.
Now, we must ensure we treat the graph as undirected: The condition should consider both orientations. Our OR covers both.
Potential pitfalls: The distances might be large, but integer operations fine.
Now, implement.
Edge Cases: Multiple edges? The input says no double edges, so each pair at most one edge.
Now, confirm with more tests.
Test 1: N=4, M=4 (a path 1-2-3-4 all weight 1). All edges are on shortest paths (since tree). So answer 0. Check algorithm: For edge (2,3), s=2,t=3 => 0+1+0=1 = dist[2][3]=1, so used. Others similar.
Test 2: N=4, M=5 (square with one diagonal). Let's label vertices 1,2,3,4 forming a cycle: edges: 1-2 (1), 2-3 (1), 3-4 (1), 4-1 (1), and diagonal 1-3 (2). All shortest paths: For pair (2,4): distances: 2-1-4 =2, 2-3-4=2, 2-1-3-4=1+2+1=4, 2-3-1-4=... So shortest=2. Edge 1-3 weight 2: check if used? For pair (1,3): dist=1? Actually direct edge weight 2, but also path 1-2-3 weight 2, 1-4-3 weight 2. So there are shortest paths of length 2. Edge 1-3 is length 2, so it is a shortest path between its endpoints. So used. For edge (2,4) not present. So all edges used? Actually all edges are either direct with weight 1 (part of many shortest paths) or the diagonal weight 2 (used as shortest between its endpoints). So answer 0. What about a heavier diagonal? e.g., 1-3 weight 10. Then dist(1,3) = 2 via 1-2-3 or 1-4-3. So edge 1-3 not used for its endpoints. Could it be used for other pairs? For pair (2,4): shortest distance = 2 (2-1-4 or 2-3-4). A path using 1-3: 2-1-3-4 = 1+10+1=12, not shortest. So not used. So answer would be 1. Check algorithm: For edge (1,3) with w=10, s=1,t=3: 0+10+0=10 !=2. For other pairs, maybe s=2,t=4? dist[2][1]=1, w=10, dist[3][4]=1 => total 12 !=2. So not used. So count 1. That matches.
Now, consider a case where an edge is not the shortest between its endpoints but is part of a shortest path for a pair where its endpoints are not the source/destination. For instance, consider a graph where two clusters connected by two parallel paths, one with heavy edges and one with light, but the heavy edge might be used in combination with very light edges to achieve a shortest path that is still less than alternatives? Actually if the heavy edge is heavy, using it would increase the distance. For it to be part of a shortest path, the alternative routes must be even heavier. So the edge must be relatively low weight compared to alternatives for that particular pair.
Example: Graph: vertices A, B, C, D. Edges: A-B (100), A-C (1), C-D (1), D-B (1). Edge A-B is heavy. Shortest path A-B is A-C-D-B = 3. So A-B edge not used for its own pair. But is it used for any other pair? Consider pair C-B: shortest path C-D-B = 2; alternative C-A-B = 1+100=101, not used. Pair A-D: shortest A-C-D = 2; alternative A-B-D = 100+1=101. So not used. Pair C-D? edge A-B not involved. So heavy edge not used. So answer includes it.
Now, consider a scenario where an edge is used as a bridge between two clusters, but there is also a direct connection between those clusters that is heavy? Actually if it's the only connection, it's used.
Thus algorithm works.
Now, about the condition: we must ensure we only consider pairs of different vertices. Already excluded s==t.
Now, about integer overflow: no.
Now, let's test on some random graphs with small N to validate manually. But we trust reasoning.
Potential alternative solution: For each edge (u,v,w), it is in some shortest path iff there exists a vertex x such that dist[x][u] + w == dist[x][v] and there exists a vertex y such that dist[y][v] + w == dist[y][u]? Not exactly.
Actually, there is known characterization: An edge (u,v,w) is used in some shortest path if and only if the condition "dist[s][u] + w + dist[v][t] == dist[s][t]" for some s,t. That's what we use.
Now, we must be careful with integer equality due to floating errors? No.
Now, implement.
But we can also use symmetry to reduce checks: For each edge, we can loop s from 0..N-1, and then we only need to check if there exists t such that either equality holds. Instead of nested t loop, we could compute for each s, whether there exists t such that dist[v][t] == dist[s][t] - (dist[s][u] + w) OR dist[u][t] == dist[s][t] - (dist[s][v] + w). That would require scanning t anyway. So nested loops fine.
Now, we can also precompute for each s,t the distance.
Implementation details: Use 0-indexing.
We should ensure to break out of both loops when used is True. We'll use a flag and break appropriately.
Alternative: Use a function with early return.
Now, let's implement with careful variable naming.
Potential speed-up: In inner loop, we can pre-fetch the row dist[s] as a local list, and rows dist[u] and dist[v] as lists. This can reduce attribute lookups.
Pseudo:
dist = [[INF]*N for _ in range(N)]
for i in range(N):
dist[i][i]=0
edges = []
for _ in range(M):
a,b,c = map(int,input().split())
a-=1; b-=1
dist[a][b] = dist[b][a] = c
edges.append((a,b,c))
# Floyd
for k in range(N):
for i in range(N):
if dist[i][k] == INF: continue
dik = dist[i][k]
for j in range(N):
nd = dik + dist[k][j]
if nd < dist[i][j]:
dist[i][j] = nd
ans = 0
for (u,v,w) in edges:
used = False
# check both orientations
for s in range(N):
dsu = dist[s][u]
dsv = dist[s][v]
# early skip? Not needed.
row_s = dist[s] # list of distances from s
row_u = dist[u] # maybe not needed for orientation 2 we need dist[u][t], but we have row_u
row_v = dist[v]
for t in range(N):
if s == t: continue
# orientation u->v
if dsu + w + row_v[t] == row_s[t]:
used = True
break
# orientation v->u
if dsv + w + row_u[t] == row_s[t]:
used = True
break
if used:
break
if not used:
ans += 1
print(ans)
But note: In the inner loop we are using row_u and row_v which are dist[u] and dist[v] lists. That's fine.
But we must ensure that for each s we recompute dsu and dsv, and fetch row_s, row_u, row_v. Actually row_u and row_v are constant per edge, so we can fetch them outside s loop to avoid repeated indexing. Similarly row_s changes per s.
Thus we can restructure:
for (u,v,w) in edges:
used = False
du = dist[u] # distances from u
dv = dist[v] # distances from v
for s in range(N):
dsu = dist[s][u]
dsv = dist[s][v]
ds = dist[s] # distances from s
for t in range(N):
if s == t: continue
if dsu + w + dv[t] == ds[t] or dsv + w + du[t] == ds[t]:
used = True
break
if used:
break
if not used:
ans += 1
This reduces some indexing.
But careful: When we break out of inner loop, we need to break outer loop. Using a flag is fine.
We can also use else-continue on loops to break outer: by using for-else, but not necessary.
Now, double-check that we haven't missed the case where s and t are swapped? Since we consider all ordered pairs, both directions covered. Because condition is symmetric in s,t? Actually the condition as we wrote uses ordered pair (s,t). For unordered pair {s,t}, we will check both orderings because the loops iterate over all s and t, including both (s,t) and (t,s). But the condition is not symmetric: For edge used in shortest path from s to t, the orientation may be s->...->u->v->...->t. So we need to check both possibilities: either the edge is traversed from u to v or from v to u. Our condition covers both because we check both ds_u + w + dv[t] and ds_v + w + du[t]. For the pair (t,s), we would also check ds' (where s'=t). But that's redundant because if edge is used in shortest path from s to t in one direction, it's also used in shortest path from t to s (reverse path). However, our loops will also catch that. But not necessary, but it's fine.
Now, confirm with a case where the edge is used only in one direction for a particular pair. For instance, consider directed? But graph is undirected, so if it's used in a shortest path from s to t, the reverse path from t to s uses the edge in opposite direction. So condition will hold for (t,s) as well via the other orientation. But even if we only check one orientation per (s,t), we might still capture it. But we check both orientations anyway.
Now, test with a small graph where the edge is only used when we consider orientation from v to u for a given (s,t). Our condition inside the inner loop checks both ds_u + w + dv[t] and ds_v + w + du[t]. So if the correct orientation is from v to u for a given s,t, the second condition will match when ds_v + w + du[t] == ds[t]. So we cover both.
Now, also consider if s = u and t = v, then ds_u = 0, dv[t] = dist[v][v] = 0, so ds_u + w + dv[t] = w, ds[t] = dist[u][v] which is w if the edge is a shortest path between u and v. So used.
If there exists a shorter alternative, then dist[u][v] < w, so condition fails.
Now, we must be careful that for s and t being the same, we skip; but we already skip s==t, but s=u,t=v are distinct (unless u=v, but no self-loops), so fine.
Now, are there any scenarios where an edge could be on a shortest path for a pair (s,t) but the decomposition as s->...->u->v->...->t is not valid because the shortest path uses the edge but the subpaths are not shortest? Actually by definition, if there is a shortest path P from s to t that includes edge (u,v) (in some direction), then the subpath from s to u (or to v, depending on orientation) is a shortest path from s to that vertex. Because if there were a shorter path from s to that vertex, then we could combine it with the rest to get a shorter path from s to t, contradicting optimality. So the distances are exact. So the condition holds.
Thus algorithm is correct.
Now, we should check if the problem considers "shortest path" as simple paths only? Usually yes, because with positive weights, shortest paths are simple.
Now, double-check constraints: M up to 1000. But we store dist as 100x100, fine.
Edge case: N=100, M=1000, but the graph may not be complete, but still connected.
Now, we can write the final solution.
But wait: There is a subtle nuance: The problem statement says "edges that are not contained in any shortest path between any pair of different vertices." Could it be that an edge is contained in a shortest path for some pair but that path uses the edge multiple times? In positive weighted graphs, a shortest path never repeats vertices because that would increase weight. So not an issue.
Now, we can also think about alternative method: After computing APSP, for each edge (u,v,w), it is used if and only if for some vertex x, dist[x][u] + w == dist[x][v] and for some vertex y, dist[y][v] + w == dist[y][u]? Actually that condition is necessary and sufficient for the edge to be in some shortest path tree? Not exactly. For it to be in some shortest path between two vertices, it's enough that there exist vertices s and t such that dist[s][u] + w + dist[v][t] == dist[s][t]. That is equivalent to there exist s and t such that dist[s][u] + w == dist[s][v] and dist[v][t] == dist[s][t] - (dist[s][u] + w)? Hmm.
But we stay with brute force.
Now, test with random small graph to ensure no oversight.
We'll implement and run mental test.
Consider N=4, edges: 1-2 (2), 1-3 (3), 2-3 (1), 3-4 (1). Compute distances:
1-2: min(2, 1+3? Actually 1-2 via 3? 1-3=3, 3-2=1 total 4, so direct 2.
1-3: min(3, 1-2-3=2+1=3) so 3 (two equal)
1-4: 1-3-4=3+1=4, 1-2-3-4=2+1+1=4, so 4.
2-3:1
2-4: 2-3-4=1+1=2
3-4:1
Now edge (1,2) weight 2: Is it used? For pair (1,2) itself, dist=2, so used. So yes.
Edge (1,3) weight 3: For pair (1,3), dist=3 (direct) so used. But also there is alternative path 1-2-3 also length 3, so it's still used as a shortest path. So used.
Edge (2,3) weight 1: used.
Edge (3,4) weight 1: used.
All used => answer 0.
Now a case where some edge not used: Let's create a graph: 1-2 (5), 1-3 (1), 2-3 (1), 2-4 (1), 3-4 (5). Distances:
1-2: min(5, 1+1=2) =>2
1-3:1
1-4: min(1+1+? Actually 1-3-4=1+5=6, 1-2-4=2+1=3, 1-3-2-4=1+1+1=3) =>3
2-3:1
2-4:1
3-4: min(5, 1+1=2) =>2
Edge (1,2) weight 5: For pair (1,2) shortest=2, so not used. Check other pairs: For (1,4): shortest=3. Could (1,2) be used? Path 1-2-4 would be 5+1=6 >3, not. For (3,4): shortest=2, path 3-2-4 =1+1=2 but uses edges (3,2) and (2,4), not (1,2). Path 3-1-2-4 =1+5+1=7 >2. So not. For (2,?) maybe? (2,? ) any? (2,?) shortest paths use edges (2,3) or (2,4) but (1,2) is only used if the source is 1 and destination is something that goes via 2? But from 1, best to 2 is via 3, not via direct. So edge (1,2) is not used. So answer should count it.
Edge (3,4) weight 5: similar not used? Check (3,4): shortest=2 via 3-2-4. So not used. For (1,4): maybe 1-3-4=1+5=6 >3, not. For (2,4): direct 1, so no. So both (1,2) and (3,4) not used. But check (1,2) again: Could there be a pair (1, something) where using (1,2) is part of a shortest path? Suppose (1,2) weight 5, but shortest from 1 to 2 is 2. So any path using (1,2) will have at least weight 5 plus something, so total >=5, while dist(1,?) could be larger than 5? For example, dist(1,4)=3, which is <5, so using (1,2) would be longer. dist(1,2)=2, <5. dist(1,3)=1 <5. So not. So yes, not used.
Thus answer 2.
Now test algorithm: For edge (1,2) w=5. Check s,t pairs. For s=1,t=2: 0+5+0=5 !=2. s=2,t=1: 0+5+0=5 !=2. s=1,t=3: 0+5+dist[2][3]=5+1=6 !=1. s=1,t=4: 0+5+dist[2][4]=5+1=6 !=3. s=2,t=3: dist[2][1]? Actually orientation: check both. For s=2,t=3: ds_u (s=2, u=1) = dist[2][1]=2? Actually dist[2][1]=2. w=5, dv[t]=dist[2][3]? Wait we need to check both formulas. Let's compute systematically:
u=1, v=2, w=5.
We need exist s,t such that dist[s][1] +5 + dist[2][t] == dist[s][t] OR dist[s][2] +5 + dist[1][t] == dist[s][t].
Check s=2,t=3: first formula: dist[2][1] +5 + dist[2][3] = 2+5+1=8, dist[2][3]=1, no. second: dist[2][2] +5 + dist[1][3] =0+5+1=6, dist[2][3]=1, no. s=3,t=4: first: dist[3][1]=1 +5 + dist[2][4]=1+5+1=7, dist[3][4]=2, no. second: dist[3][2]=1 +5 + dist[1][4]=1+5+3=9, no. So none. So algorithm returns not used. Good.
Now, there is also possibility that the edge could be used for pair (something, something) where both s and t are not the endpoints, but the edge is used in the middle. Example earlier: edge B-C used for pair B-D in the square with diagonal? Actually earlier we had square with diagonal 1-3 heavy? Not used. But another example: consider a graph where there are two parallel