Bellman Ford(warm up-2 1005)

 

 数据弱,代码有问题,等待修正

 

#include <iostream>
using namespace std;

__int64 lmax
=0x7FFFFFFF;
const long MAXN=1200;

__int64 hash[MAXN][MAXN];
__int64 Dis[MAXN];

long Start,End;
long m,n;//点,边

int main()
{
    lmax
=lmax<<10;
    
long T;
    scanf(
"%ld",&T);

    
while (T--)
    {
        scanf(
"%ld",&m);
        scanf(
"%ld %ld",&Start,&End);
        scanf(
"%ld",&n);
        
long i,ii;
        
for (i=0;i<MAXN;++i)
        {
            Dis[i]
=lmax;
            
for (ii=0;ii<MAXN;++ii)
            {
                hash[i][ii]
=lmax;
            }
        }
        
for (i=0;i<n;++i)
        {
            
long from,to;
            __int64 cost;
            scanf(
"%ld %ld %I64d",&from,&to,&cost);
        
            
if (cost<hash[from][to])
            {
                hash[from][to]
=cost;
            }
        }

        Dis[Start]
=0;

        
long j,k;
        
for (i=1;i<m;++i)
        {
            
bool doit=false;
            
for (j=0;j<m;++j)
            {
                
for (k=0;k<m;++k)
                {
                    
if (hash[j][k]!=lmax&&Dis[j]<lmax&&Dis[j]+hash[j][k]<Dis[k])
                    {
                        Dis[k]
=Dis[j]+hash[j][k];
                        doit
=true;
                    }
                }
            }
            
if (!doit)
            {
                
break;
            }
        }

        
bool Y=true;
        
for (j=0;j<m;++j)
        {
            
for (k=0;k<m;++k)
            {
                
if (Dis[j]<lmax&&Dis[j]+hash[j][k]<Dis[k])
                {
                    Y
=false;
                    
goto l1;
                }
            }
        }
l1:


        
if (Dis[End]==lmax||!Y)
        {
            printf(
"infinity\n");
        }
        
else
        {
            printf(
"%I64d\n",Dis[End]);
        }

    }
    
return 0;
}
posted @ 2008-08-23 15:16  Hdu-Lost  阅读(212)  评论(0编辑  收藏  举报