博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

P100 单源最短路问题 Bellman-Ford 算法

Posted on 2016-03-02 20:04  shaoweiyi  阅读(119)  评论(0编辑  收藏  举报
///单源最短路问题

///DAG:单向不循环图  

///问题的特殊性:要对变进行遍历,而不是顶点

const int MAX_V=; const int MAX_E=; const int INF=; int num_v; int num_e; int start; int aim; struct edge { int from; int to; int cost; }; edge G[MAX_E]; int dis[MAX_V]; void min_path(int start) { for(int i=0;i<num_v;++i) dis[i]=INF; dis[start]=0; ///按照边循环还是挺暴力的。 while(true) { int flag=0; for(int i=0;i<num_e;++i) { if( dis[ G[i].from ]+G[i].cost < dis[ G[i].to ] ) { dis[ G[i].to ] =dis[ G[i].from ]+G[i].cost flag=1; } } if(!flag) break; } }