using
System;
using
System.Collections.Generic;
class
ShortestPaths
{
const
int
INF =
int
.MaxValue;
const
int
MAXN = 100001;
static
List<List<Tuple<
int
,
int
>>> g =
new
List<List<Tuple<
int
,
int
>>>(MAXN);
static
List<
int
> dist =
new
List<
int
>(MAXN);
static
List<
int
> route =
new
List<
int
>(MAXN);
static
void
CountDistinctShortestPaths(
int
n,
int
m,
int
[,] edges)
{
for
(
int
i = 0; i < n + 1; ++i)
{
g.Add(
new
List<Tuple<
int
,
int
>>());
dist.Add(INF);
route.Add(0);
}
for
(
int
i = 0; i < m; ++i)
{
int
u = edges[i, 0];
int
v = edges[i, 1];
int
c = edges[i, 2];
g[u].Add(
new
Tuple<
int
,
int
>(v, c));
}
var
pq =
new
SortedSet<Tuple<
int
,
int
>>(Comparer<Tuple<
int
,
int
>>.Create((x, y) =>
{
int
compare = x.Item1.CompareTo(y.Item1);
return
compare == 0 ? x.Item2.CompareTo(y.Item2) : compare;
}));
pq.Add(
new
Tuple<
int
,
int
>(0, 1));
dist[1] = 0;
route[1] = 1;
while
(pq.Count > 0)
{
var
top = pq.Min;
pq.Remove(top);
int
d = top.Item1;
int
u = top.Item2;
if
(d > dist[u])
continue
;
foreach
(
var
e
in
g[u])
{
int
v = e.Item1;
int
c = e.Item2;
if
(c + d > dist[v])
continue
;
if
(c + d == dist[v])
{
route[v] += route[u];
}
if
(c + d < dist[v])
{
dist[v] = c + d;
route[v] = route[u];
pq.Add(
new
Tuple<
int
,
int
>(dist[v], v));
}
}
}
}
static
void
Main()
{
int
n = 4;
int
m = 5;
int
[,] edges = { { 1, 4, 5 },
{ 1, 2, 4 },
{ 2, 4, 5 },
{ 1, 3, 2 },
{ 3, 4, 3 } };
CountDistinctShortestPaths(n, m, edges);
Console.WriteLine(route[n]);
}
}