| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
我的做法是:In Reply To:Re:FT! SPFA47ms bellman-fod16ms Posted by:bugthink at 2010-03-18 10:30:06 不能循环|V|次(也没必要,在最坏情况下,才会那样的),那样太慢了。
我的方法是:
int bound=|V|;boolean change;
do{
change=false;
for(Edge e :edges)
change|=relax(e.u,e.v,e.w);/*function relax returns true when relaxation did happen*/
bound--;
}while(change&&bound>0);
if(change)return false;//infeasible
else return true;//feasible
说明:这样,如果没到|V|-1次就发现无法再relax任何顶点,就可以提前退出loop。
如果有负权回路,那么relax操作可以一直执行下去(就是无法收敛),所以,“relax所有边”|V|-1次还能relax,就说明已经有负权回路,但是,这时(“relax所有边”|V|次后,bound已经为0),所以,跳出loop,但是,change为true,所以,最终判断是,有负权回路。
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator