hdu 2680 Choose the best route最短路。。

最近才发现最短路的算法实在是太多了,就比如对于这道题目,可以用Dijkstra和优先队列

Dijkstra又可以用多源做 和 反向图+单源的!

优先队列类似于这个Bellman-Ford;

至于什么神马spfa估计也是和这个优先队列差不多。。 

我一直都比较喜欢这个dijkstra,这次也是用这个来做的,直接用多源。。

题目意思大家都能看懂吧  ^_^

贴代码:

  
    
# include < stdio.h >
# include
< string .h >
# define PI
0xfffffff
int adj[ 1005 ][ 1005 ],low[ 1005 ],visit[ 1005 ];
int main()
{
int i,m,n,w,s,p,q,t,index,index1,min,x;
while (scanf( " %d%d%d " , & n, & m, & s) != EOF)
{
memset(adj,
- 1 , sizeof (adj));
for (i = 1 ;i <= m;i ++ )
{
scanf(
" %d%d%d " , & p, & q, & t);
if (adj[p][q] ==- 1 || adj[p][q] > t) adj[p][q] = t;
}
scanf(
" %d " , & w);
for (i = 1 ;i <= n;i ++ )
{
visit[i]
= 0 ;
low[i]
= PI;
}
for (i = 1 ;i <= w;i ++ )
{
scanf(
" %d " , & x);
low[x]
= 0 ;
}
index
= x;
while (index != s)
{
min
= PI;
visit[index]
= 1 ;
for (i = 1 ;i <= n;i ++ )
{
if (visit[i] == 1 ) continue ;
if (adj[index][i] ==- 1 && low[i] == PI) continue ;
if (adj[index][i] !=- 1 )
{
if (low[i] ==- 1 || adj[index][i] + low[index] < low[i]) low[i] = adj[index][i] + low[index];
}
if (low[i] < min){min = low[i];index1 = i;}
}
if (min == PI) break ;
index
= index1;
}
if (low[s] == PI) printf( " -1\n " );
else printf( " %d\n " ,low[s]);
}
return 0 ;
}

你可能感兴趣的:(route)