Floyd-Warshall算法是求解任意两点最短路的有力武器。其也适用于存在负边的情况。
DP思路,假定只使用前K个点时i到j的最短距离为d[k][i][j]
那末,使用前K+1个点就能够分成两种情况
①i到j的最短路用到了第K+1个点(d[k+1][i][j] = d[k][i][j])
②i到j的最短路没有用到第K+1个点(d[k+1][i][j] = d[k][i][k]+d[k][k][j]);
所以,d[k+1][i][j] = min(d[k][i][j],d[k][i][k]+d[k][k][j])
使用转动数组,就能够写成2维数组的情势
d[i][j] = min(d[i][j],d[i][k]+d[k][j])
Floyd算法可以在O(V^3)的时间内解出,判断图中是不是有负圈,只需要检查是不是存在d[i][i]是负数的顶点i就能够了。
代码
//d[i][j]表示i->j的权值,不存在时设为INF 但是d[i][i]设为0
for(int k = 0 ; k < V ; k ++) {
for(int i = 0 ; i < V ; i ++) {
for(int j = 0 ; j < V ; j ++) {
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
}
}
由于实现起来非常简单,如果复杂度在可以承受的范围以内,单源最短路也能够使用Floyd-Warshall算法来进行求解。
在有向图中,如果需要判断每两个点是不是存在路径,那末也能够用Floyd算法来进行传递闭包。
只需要改成d[i][j] = d[i][j] || (d[i][k]&&d[k][j])便可(预处理也要变化)
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » 任意两点最短路 Floyd-Warshall算法 传递闭包
波比源码 » 任意两点最短路 Floyd-Warshall算法 传递闭包
levofloxacin uk levofloxacin 500mg pill
lamotrigine 50mg pills order retin without prescription order retin
accutane 40mg generic azithromycin tablet ivermectin over counter
buy generic buspirone 5mg buspirone 5mg pill ditropan online
buy prograf pill buy trandate 100mg without prescription order urso sale
oral sertraline 50mg lexapro 10mg uk order viagra pills
purchase azithromycin pill order prednisolone order neurontin 800mg online
furosemide 40mg price buy lasix 40mg order plaquenil 400mg generic
metformin drug glycomet 1000mg oral cialis medication
order metoprolol online levitra 20mg sale buy levitra 10mg for sale
buy vardenafil sale purchase vardenafil methylprednisolone 8mg without prescription
brand cialis 40mg tadalafil ca real viagra sites
aristocort 10mg us loratadine 10mg uk desloratadine 5mg over the counter
order zyloprim pill zetia 10mg pills zetia drug
methotrexate 2.5mg oral methotrexate price metoclopramide without prescription
order baclofen 25mg online cheap tizanidine cheap buy ketorolac
tamsulosin cost flomax cost order aldactone 25mg sale
viagra order online buy sildenafil 100mg generic cialis 10mg uk
buy deltasone 40mg pills order amoxicillin 500mg online cheap amoxil 1000mg for sale
cheap viagra pill viagra 100 mg purchase cialis without prescription
buy lasix pill order generic doxycycline 100mg order hydroxychloroquine generic
purchase lamisil without prescription cheap terbinafine 250mg trimox 500mg ca
buy naprosyn 250mg generic buy prevacid 15mg generic buy lansoprazole without prescription
cheap cialis sale best over the counter ed pills tadalafil brand name
gambling games real online blackjack online blackjack gambling
clobetasol cream clobetasol medication buy amiodarone 100mg for sale
olmesartan 10mg us divalproex 250mg usa order divalproex 250mg pill
generic acetazolamide 250 mg order azathioprine 50mg generic imuran 25mg pill
cost lanoxin generic telmisartan order molnupiravir without prescription
buy carvedilol 25mg without prescription buy elavil 10mg sale buy elavil 10mg generic
order amoxicillin generic purchase stromectol online cheap ivermectin 0.5%
order dapoxetine 90mg without prescription dapoxetine pills buy domperidone 10mg generic
order pamelor 25 mg online cheap order paroxetine 10mg paroxetine 10mg uk
cheap indocin 50mg buy indocin without prescription cenforce 100mg for sale
buy doxycycline 200mg online methylprednisolone cost methylprednisolone cost in usa
brand tadalafil 20mg amoxicillin 250mg tablet purchase amoxicillin pills
nexium 20mg for sale clarithromycin pills buy lasix 40mg online cheap
buy cialis 40mg pills Cialis delivery sildenafil 100mg pills
minocycline medication neurontin 800mg canada buy terazosin 1mg generic
glucophage 500mg drug verapamil 120mg over the counter nolvadex sale
order provigil 200mg online cheap promethazine medication phenergan ca
clomid 50mg us order clomid 100mg for sale order generic prednisolone 10mg
buy deltasone 10mg online cheap buy accutane 40mg pills cost amoxicillin 1000mg
buy accutane online cheap acillin order ampicillin 500mg sale
fildena 50mg price cheapest ed pills online propecia 5mg uk
ivermectin tablets uk prednisone order online buy deltasone 5mg generic
purchase albuterol inhaler levothroid ca clavulanate price