Ural 1004 Sightseeing trip

Ural 1004 Sightseeing trip
第一次提交就WA了,而且在第一次测试点就WA了!瞅了半天,没看出来哪错……最后发现,"No solution"少了一个"."!无语无语……淡定淡定……
看别人用的是Floyed求最小环,确实很方便!学习了!
我用的是枚举起点终点+Dijkstra的方法。
以下是我的代码:
#include < iostream >
#include
< string .h >
#define  maxn 107
#define  INF 20000007
using   namespace  std;
long  n,m,ans,num,g[maxn][maxn],tmp[maxn],path[maxn];
long  dijkstra( long  begin, long  end)
{
    
long  min,minp,d[maxn];
    
bool  used[maxn];
    
for ( long  i = 1 ;i <= n;i ++ )
        d[i]
= (i == begin ? 0 :INF);
    memset(used,
false , sizeof (used));
    memset(tmp,
0 , sizeof (tmp));
    
    
for ( long  i = 1 ;i <= n;i ++ )
    {
        min
= INF;minp = 0 ;
        
for ( long  j = 1 ;j <= n;j ++ )
            
if ( ! used[j] && min > d[j])
            {
                min
= d[j];minp = j;
            }
        used[minp]
= true ;
        
for ( long  j = 1 ;j <= n;j ++ )
            
if ( ! used[j] && d[j] > min + g[minp][j])
            {
                d[j]
= min + g[minp][j];
                tmp[j]
= minp;
            }
    }
    
return  d[end];
}
int  main()
{
    
while (cin >> n >> &&  n !=- 1 )
    {
        
for ( long  i = 1 ;i <= n;i ++ )
            
for ( long  j = 1 ;j <= n;j ++ )
                g[i][j]
= INF;
        
for ( long  i = 1 ;i <= m;i ++ )
        {
            
long  u,v,w;
            cin
>> u >> v >> w;
            
if (g[u][v] > w)
                g[u][v]
= g[v][u] = w;
        }
        
//   Input
        ans = INF;
        memset(path,
0 , sizeof (path));
        
//   Init
         for ( long  i = 1 ;i <= n;i ++ )
            
for ( long  j = i + 1 ;j <= n;j ++ )
            {
                
long  t = g[i][j],now;
                g[i][j]
= g[j][i] = INF;
                now
= dijkstra(i,j) + t;
                
if (ans > now)
                {
                    ans
= now;
                    
//   Update
                    num = 0 ;
                    memset(path,
0 , sizeof (path));
                    
long  r = j;
                    
while (r)
                    {
                        num
++ ;
                        path[num]
= r;
                        r
= tmp[r];
                    }
                    
//   Deal path
                }
                g[i][j]
= g[j][i] = t;
            }
        
//   Find the shorest path
         if (ans >= INF)
            cout
<< " No solution. " << endl;
        
else
        {
            
for ( long  i = num;i >= 1 ;i -- )
            {
                
if (i != num) cout << "   " ;
                cout
<< path[i];
            }
            cout
<< endl;
        }
    }
return   0 ;
}


你可能感兴趣的:(Ural 1004 Sightseeing trip)