Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我的做法是:

Posted by bugthink at 2010-03-18 10:42:32 on Problem 1275
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator