-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 59 - possiblePaths.py
47 lines (34 loc) · 1.22 KB
/
Day 59 - possiblePaths.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Given a directed graph and two vertices ‘u’ and ‘v’, find the number of possible walks from ‘u’ to ‘v’ with exactly k edges modulo 109+7
# Day 59/100
def MinimumWalk(graph, u, v, k):
V = len(graph)
MOD = 1000000007
count = [[[0 for k in range(k + 1)] for i in range(V)] for j in range(V)]
for e in range(0, k + 1):
for i in range(V):
for j in range(V):
if e == 0 and i == j:
count[i][j][e] = 1
if e == 1 and graph[i][j] != 0:
count[i][j][e] = 1
if e > 1:
for a in range(V):
if graph[i][a] != 0:
count[i][j][e] += count[a][j][e - 1]
count[i][j][e] = count[i][j][e] % MOD
return count[u][v][k] % MOD
# Driver Code Starts
if __name__ == "__main__":
T = int(input())
for i in range(T):
n = int(input())
graph = []
for i in range(n):
a = list(map(int, input().split()))
graph.append(a)
u, v, k = input().split()
u = int(u)
v = int(v)
k = int(k)
ans = MinimumWalk(graph, u, v, k)
print(ans)