using
System;
using
System.Collections.Generic;
public
class
GFG {
static
int
countPaths(
int
n, List<List<
int
>> edges) {
double
mod = 1e9 + 7;
List<List<
int
[]>> graph =
new
List<List<
int
[]>>();
for
(
int
i = 0; i <= n; i++) {
graph.Add(
new
List<
int
[]>());
}
foreach
(List<
int
> edge
in
edges) {
graph[edge[0]].Add(
new
int
[] { edge[1], edge[2] });
graph[edge[1]].Add(
new
int
[] { edge[0], edge[2] });
}
long
[] distance =
new
long
[n];
Array.Fill(distance,
long
.MaxValue);
int
[] path =
new
int
[n];
PriorityQueue<
long
[]> pq =
new
PriorityQueue<
long
[]>((x, y) => (
int
)(x[0] - y[0]));
pq.Enqueue(
new
long
[] { 0, 0 });
distance[0] = 0;
path[0] = 1;
while
(pq.Count() != 0) {
long
[] t = pq.Dequeue();
int
vertex = (
int
)t[1];
foreach
(
int
[] nbr
in
graph[vertex]) {
int
nbrVertex = nbr[0];
int
nbrEdge = nbr[1];
if
(distance[nbrVertex] > distance[vertex] + nbrEdge) {
distance[nbrVertex] = distance[vertex] + nbrEdge;
pq.Enqueue(
new
long
[] { distance[nbrVertex], nbrVertex });
path[nbrVertex] = path[vertex] % 1000000007;
}
else
if
(distance[nbrVertex] == t[0] + nbrEdge) {
path[nbrVertex] += path[vertex];
path[nbrVertex] %= 1000000007;
}
}
}
return
path[n - 1];
}
public
static
void
Main(
string
[] args) {
int
n = 7;
List<List<
int
>> edges =
new
List<List<
int
>> {
new
List<
int
> { 0, 6, 7 },
new
List<
int
> { 0, 1, 2 },
new
List<
int
> { 1, 2, 3 },
new
List<
int
> { 1, 3, 3 },
new
List<
int
> { 6, 3, 3 },
new
List<
int
> { 3, 5, 1 },
new
List<
int
> { 6, 5, 1 },
new
List<
int
> { 2, 5, 1 },
new
List<
int
> { 0, 4, 5 },
new
List<
int
> { 4, 6, 2 }
};
int
numPaths = countPaths(n, edges);
Console.WriteLine(
"Number of paths from 0 to "
+ (n - 1) +
": "
+ numPaths);
}
}
public
class
PriorityQueue<T>
{
private
List<T> data;
private
Comparison<T> comparison;
public
PriorityQueue(Comparison<T> comparison)
{
this
.data =
new
List<T>();
this
.comparison = comparison;
}
public
void
Enqueue(T item)
{
data.Add(item);
int
i = data.Count - 1;
while
(i > 0)
{
int
j = (i - 1) / 2;
if
(comparison(data[j], item) <= 0)
break
;
data[i] = data[j];
i = j;
}
data[i] = item;
}
public
T Dequeue()
{
int
lastIndex = data.Count - 1;
T frontItem = data[0];
data[0] = data[lastIndex];
data.RemoveAt(lastIndex);
--lastIndex;
int
i = 0;
while
(
true
)
{
int
childIndex = i * 2 + 1;
if
(childIndex > lastIndex)
break
;
int
rightChild = childIndex + 1;
if
(rightChild <= lastIndex && comparison(data[rightChild], data[childIndex]) < 0) childIndex = rightChild;
if
(comparison(data[i], data[childIndex]) <= 0)
break
;
T tmp = data[i]; data[i] = data[childIndex]; data[childIndex] = tmp;
i = childIndex;
}
return
frontItem;
}
public
T Peek()
{
T frontItem = data[0];
return
frontItem;
}
public
int
Count()
{
return
data.Count;
}
}