hdu1874(畅通工程续)

http://acm.hdu.edu.cn/showproblem.php?pid=1874

View Code
   
     
#include < iostream >
#define _max 9999999
#define M 201
using namespace std;
int map[M][M];
int visit[M];
int dir[M];
int n , m ;
int a , b , c ;
int len;
void Init( int n)
{
int i,j;
for (i = 0 ; i < n ; i ++ )
{
for (j = 0 ; j < n; j ++ )
map[i][j]
= _max;
}
}
void DJ( int v)
{
int i,j,p,k ,_min;
for (i = 0 ;i <= n;i ++ )
dir[i]
= map[v][i];
dir[v]
= 0 ;
visit[v]
= true ;
for (i = 0 ;i <= n;i ++ )
{
_min
= _max;
for (j = 0 ;j < n;j ++ )
{
if ( ! visit[j] && _min > dir[j])
{
_min
= dir[j];
k
= j;
}
}
visit[k]
= true ;
for ( p = 0 ; p < n; p ++ )
{
if ( ! visit[p] && dir[p] > dir[k] + map[k][p])
{
dir[p]
= dir[k] + map[k][p];
}
}
}
}
int main()
{

while (cin >> n >> m)
{
Init(n);
int i;
int s,e;
for (i = 0 ;i < m;i ++ )
{
cin
>> a >> b >> c;
if (map[a][b] > c) map[a][b] = map[b][a] = c;

}
memset(visit ,
false , sizeof (visit));
cin
>> s >> e;
DJ(s);
if (dir[e] < _max)cout << dir[e] << endl;
else cout << " -1 " << endl;
}
return 0 ;

}

你可能感兴趣的:(HDU)