|
1 |
| -import type { IEdge, IGraph } from '../src' |
| 1 | +import { testOjCodes } from 'jest.setup' |
| 2 | +import type { IGraph } from '../src' |
2 | 3 | import bellmanFord from '../src'
|
3 | 4 |
|
4 | 5 | describe('basic', function () {
|
@@ -38,94 +39,16 @@ describe('basic', function () {
|
38 | 39 | })
|
39 | 40 | })
|
40 | 41 |
|
41 |
| -describe('leetcode', function () { |
| 42 | +describe('oj', function () { |
42 | 43 | // https://leetcode.com/problems/number-of-ways-to-arrive-at-destination/
|
43 |
| - test('leetcode/number-of-ways-to-arrive-at-destination', function () { |
44 |
| - const data: any[] = [ |
45 |
| - { |
46 |
| - input: [ |
47 |
| - 7, |
48 |
| - [ |
49 |
| - [0, 6, 7], |
50 |
| - [0, 1, 2], |
51 |
| - [1, 2, 3], |
52 |
| - [1, 3, 3], |
53 |
| - [6, 3, 3], |
54 |
| - [3, 5, 1], |
55 |
| - [6, 5, 1], |
56 |
| - [2, 5, 1], |
57 |
| - [0, 4, 5], |
58 |
| - [4, 6, 2], |
59 |
| - ], |
60 |
| - ], |
61 |
| - answer: 4, |
62 |
| - }, |
63 |
| - { |
64 |
| - input: [2, [[1, 0, 10]]], |
65 |
| - answer: 1, |
66 |
| - }, |
67 |
| - ] |
68 |
| - |
69 |
| - const customDist: number[] = [] |
70 |
| - for (const kase of data) { |
71 |
| - const [N, roads] = kase.input |
72 |
| - |
73 |
| - expect(countPaths(N, roads)).toEqual(kase.answer) |
74 |
| - expect(countPaths(N, roads, customDist)).toEqual(kase.answer) |
75 |
| - } |
76 |
| - }) |
| 44 | + testOjCodes( |
| 45 | + 'leetcode/number-of-ways-to-arrive-at-destination', |
| 46 | + import('./oj/number-of-ways-to-arrive-at-destination'), |
| 47 | + ) |
| 48 | + |
| 49 | + // https://leetcode.com/problems/maximum-path-quality-of-a-graph/ |
| 50 | + testOjCodes( |
| 51 | + 'leetcode/maximum-path-quality-of-a-graph', |
| 52 | + import('./oj/maximum-path-quality-of-a-graph'), |
| 53 | + ) |
77 | 54 | })
|
78 |
| - |
79 |
| -const MOD = 1e9 + 7 |
80 |
| -function countPaths(N: number, roads: number[][], customDist?: number[]): number { |
81 |
| - const edges: IEdge[] = [] |
82 |
| - const G: number[][] = new Array(N) |
83 |
| - for (let i = 0; i < N; ++i) G[i] = [] |
84 |
| - for (const [from, to, cost] of roads) { |
85 |
| - G[from].push(edges.length) |
86 |
| - edges.push({ to, cost }) |
87 |
| - |
88 |
| - G[to].push(edges.length) |
89 |
| - edges.push({ to: from, cost }) |
90 |
| - } |
91 |
| - |
92 |
| - const source = 0 |
93 |
| - const target = N - 1 |
94 |
| - const graph: IGraph = { |
95 |
| - N, |
96 |
| - source: target, |
97 |
| - edges, |
98 |
| - G, |
99 |
| - dist: customDist ?? [], |
100 |
| - } |
101 |
| - |
102 |
| - bellmanFord(graph, { INF: 1e12 }) |
103 |
| - const { dist } = graph |
104 |
| - |
105 |
| - const dp: number[] = new Array(N).fill(-1) |
106 |
| - return dfs(source) |
107 |
| - |
108 |
| - function dfs(o: number): number { |
109 |
| - if (o === target) return 1 |
110 |
| - |
111 |
| - let answer = dp[o] |
112 |
| - if (answer !== -1) return answer |
113 |
| - |
114 |
| - answer = 0 |
115 |
| - const d = dist[o] |
116 |
| - for (const idx of G[o]) { |
117 |
| - const e: IEdge = edges[idx] |
118 |
| - if (dist[e.to] + e.cost === d) { |
119 |
| - const t = dfs(e.to) |
120 |
| - answer = modAdd(answer, t) |
121 |
| - } |
122 |
| - } |
123 |
| - dp[o] = answer |
124 |
| - return answer |
125 |
| - } |
126 |
| -} |
127 |
| - |
128 |
| -function modAdd(x: number, y: number): number { |
129 |
| - const z: number = x + y |
130 |
| - return z < MOD ? z : z - MOD |
131 |
| -} |
0 commit comments