HDU1874 畅通工程续 单源最短路径

HDU1874 畅通工程续 单源最短路径
最简单的dijkstra应用,别的就不说了 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1874
#include < stdio.h >
#include
< string .h >
#define  MAXN 201
#define  MAX 999999
int  dist[MAXN][MAXN],x[MAXN];
int  main()
{
    
int  n,m,i,j,t,s,d,a,b,time,min;
    
while (scanf( " %d%d " , & n, & m) == 2 )
    {
        
for (i = 0 ;i < n;i ++ )
        {
            
for (j = 0 ;j < n;j ++ )
            {
                
if (i == j)
                    dist[i][j]
= 0 ;
                
else
                    dist[i][j]
= MAX;
            }
        }
        
for (i = 0 ;i < m;i ++ )
        {
            scanf(
" %d%d%d " , & a, & b, & time);
            dist[a][b]
= dist[b][a] = time < dist[a][b] ? time:dist[a][b];
        }
        scanf(
" %d%d " , & s, & d);
        
for (i = 0 ;i < n;i ++ )
        x[i]
= dist[s][i];
        dist[s][s]
= 1 ;
        t
= 0 ;
        
while (t !=- 1 )
        {
            min
=- 1 ;
            t
=- 1 ;
            
for (i = 0 ;i < n;i ++ )
            
if (dist[i][i] == 0 && (min ==- 1 || x[i] < min))
            {
                min
= x[i];
                t
= i;
            }
            
if (t !=- 1 )
            {
                dist[t][t]
= 1 ;
                
for (i = 0 ;i < n;i ++ )
                
if (x[i] > x[t] + dist[t][i])
                {
                    x[i]
= x[t] + dist[t][i];
                }
            }
        }
        
if (x[d] < MAX)
            printf(
" %d\n " ,x[d]);
        
else
            printf(
" -1\n " );
    }
    
return   0 ;
}

你可能感兴趣的:(HDU1874 畅通工程续 单源最短路径)