当前位置:网站首页>Djikstra solves the shortest circuit with negative weight

Djikstra solves the shortest circuit with negative weight

2022-06-11 07:29:00 Master. Yi

This article blog Very good

Set a potential function at each point h i h_i hi, And let the edge weight of the new graph w i , j ′ = w i , j + h i − h j w'_{i,j}=w_{i,j}+h_i-h_j wi,j=wi,j+hihj, requirement w i , j ′ ≥ 0 w'_{i,j}\ge 0 wi,j0
Original picture 1 → n 1\to n 1n The shortest path of is equal to the new graph 1 → n 1\to n 1n The shortest path of minus h 1 − h n h_1-h_n h1hn

How to maintain h i h_i hi
Yes h i h_i hi For any edge with flow ( i , j ) (i,j) (i,j), h i + w i , j ≥ h j h_i+w_{i,j}\ge h_j hi+wi,jhj
This is similar to the shortest path trigonometric inequality d i s i + w i , j ≥ d i s j dis_i+w_{i,j}\ge dis_j disi+wi,jdisj
At the beginning, if the edge weight is not negative, it is all set 0
Otherwise, you can run once SPFA, Make h i = d i s i h_i=dis_i hi=disi. If it's a DAG You can push h j = min ⁡ h i + w i , j h_j=\min h_i+w_{i,j} hj=minhi+wi,j.

After running Zengguang Road, some edges will be added , Suppose that ( i , j ) (i,j) (i,j), Produced ( j , i ) (j,i) (j,i)
First of all, there must be d i s i + h i − h j + w i , j = d i s j dis_i+h_i-h_j+w_{i,j}=dis_j disi+hihj+wi,j=disj ( With S S S For the source point )
Change to ( d i s i + h i ) + w i , j = ( d i s j + h j ) (dis_i+h_i)+w_{i,j}=(dis_j+h_j) (disi+hi)+wi,j=(disj+hj)
So make the new h i ′ = h i + d i s i h_i'=h_i+dis_i hi=hi+disi To satisfy the newly added edge .

And for the original edge , Because of d i s i + h i − h j + w i , j ≥ d i s j dis_i+h_i-h_j+w_{i,j}\ge dis_j disi+hihj+wi,jdisj, Easy to get h i ′ h_i' hi Still meet the conditions .

If the T T T Take the shortest path of the reverse side for the source point , Make w i , j ′ = w i , j + h j − h i w'_{i,j}=w_{i,j}+h_j-h_i wi,j=wi,j+hjhi that will do , h i ′ h_i' hi Still add... Every time d i s i dis_i disi.

原网站

版权声明
本文为[Master. Yi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020520543243.html