良い例として、次の問題に役立ちます。
正の重みと N 個の頂点を持つ無向グラフ G が与えられます。
あなたはMのお金の合計を持つことから始めます。頂点 i を通過するには、S[i] のお金を支払わなければなりません。十分なお金がなければ、その頂点を通過できません。上記の条件を考慮して、頂点 1 から頂点 N への最短経路を見つけます。または、そのようなパスは存在しないと述べます。同じ長さのパスが複数存在する場合は、最も安いパスを出力します。制限: 1
擬似コード:
Set states(i,j) as unvisited for all (i,j)
Set Min[i][j] to Infinity for all (i,j)
Min[0][M]=0
While(TRUE)
Among all unvisited states(i,j) find the one for which Min[i][j]
is the smallest. Let this state found be (k,l).
If there wasn't found any state (k,l) for which Min[k][l] is
less than Infinity - exit While loop.
Mark state(k,l) as visited
For All Neighbors p of Vertex k.
If (l-S[p]>=0 AND
Min[p][l-S[p]]>Min[k][l]+Dist[k][p])
Then Min[p][l-S[p]]=Min[k][l]+Dist[k][p]
i.e.
If for state(i,j) there are enough money left for
going to vertex p (l-S[p] represents the money that
will remain after passing to vertex p), and the
shortest path found for state(p,l-S[p]) is bigger
than [the shortest path found for
state(k,l)] + [distance from vertex k to vertex p)],
then set the shortest path for state(i,j) to be equal
to this sum.
End For
End While
Find the smallest number among Min[N-1][j] (for all j, 0<=j<=M);
if there are more than one such states, then take the one with greater
j. If there are no states(N-1,j) with value less than Infinity - then
such a path doesn't exist.