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

Bellman-Ford 水过 (用Java的)

Posted by HeartLC at 2013-04-05 22:51:20 on Problem 3259
这道题目,普通洞是双向的,虫洞是单向的。
然后,只要判断是不是有负回路就可以了,所以,直接用《算导》里面的Bellman-Ford就可以了。
Bellman-Ford算法,是能够判断出图上是不是用负回路的,无论从哪个点开始初始化都可以,所以,就只要执行一次Bellman-Ford,就可以看出该图是不是有负回路了。


import java.util.Scanner;

public class Main
{
       private class Path
       {
               public int s1;
               public int s2;
               public int length;
               
               Path( int s1,int s2,int length ) 
               {
                     this.s1 = s1;
                     this.s2 = s2;
                     this.length = length;
               }
       }
       private int n;      //N个洞
       private int m;      //M条路
       private int w;      //W个虫洞
       private Path[] ps;    //ps为所有的路径,包括虫洞的
       private int[] d;
       Main( int n,int m,int w )        //初始化
       {
             this.n = n;
             this.m = m;
             this.w = w;
             this.ps = new Path[2*m+w];
             this.d = new int[n+1];
       }
       public void setPath( int i,int s1,int s2,int length )
       {
              this.ps[i] = new Path( s1,s2,length );
       }
       private void initializtion( int s )
       {
               for( int i=1;i<n;i++ )
               {
                    d[i] = 65555;
               }
               d[s] = 0;
       }
       private void Relax( Path path )
       {
               if( this.d[path.s2] > this.d[path.s1] + path.length )
               {
                   this.d[path.s2] = this.d[path.s1] + path.length;
               }
       }
       private boolean BellmanFord( int s )       //主要是判断是不是有回路
       {
               this.initializtion(s);
               for( int i=1;i<=n;i++ )
               {
                    for( int j=0;j<(2*m+w);j++ )
                    {
                         this.Relax( this.ps[j] );
                    }                 
               }
               for( int i=0;i<(2*m+w);i++ )
               {
                    Path path = this.ps[i];
                    if( d[path.s2] > d[path.s1] + path.length )
                    {
                         return true;
                    }
               }
               return false;
       }
       public void judge()
       {
              
                   if( this.BellmanFord( 1 ) )       //只要从一个顶点开始,有一条负的回路,就可以在里面无限次循环,也就可以判断可以回到目标
                   {
                       System.out.println( "YES" );
                       return;
                   }
              
              
              System.out.println( "NO" );
              
       }
       public static void main(String[] args)
       {
              Scanner s = new Scanner(System.in);
              int f = s.nextInt();     //F张地图
              while( f>0 )
              {
                     int n = s.nextInt();
                     int m = s.nextInt();
                     int w = s.nextInt();
                     Main main = new Main( n,m,w );
                     int i=0;
                     for( i=0;i<2*m;i+=2 )
                     {
                          int s1 = s.nextInt();
                          int s2 = s.nextInt();
                          int length = s.nextInt();
                          main.setPath( i,s1,s2,length );
                          main.setPath( i+1,s2,s1,length );
                     }
                     for( int j=0;j<w;j++ )
                     {
                          int s1 = s.nextInt();
                          int s2 = s.nextInt();
                          int length = s.nextInt();
                          main.setPath( i+j,s1,s2,0-length );
                     }
                     main.judge();
                     f--;
              }
       }

}

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