hdu 1142 A Walk Through the Forest
2013-08-31 10:31
483 查看
分析:从始点出发,走到目标点,每走一步,只能是离目标点越来越近的路,问这样的路有几条。
以目标点为源点用dijkstra求出源点到其他各个顶点的最短路径,然后利用广搜搜索,满足Edge[v0][i] < INF && dist[v0]>dist[i] 条件,Edge[v0][i] < INF 表示v0到某点i有直接路径(即边)dist[i] < dist[v0] 表示离目标点越走越近。
以目标点为源点用dijkstra求出源点到其他各个顶点的最短路径,然后利用广搜搜索,满足Edge[v0][i] < INF && dist[v0]>dist[i] 条件,Edge[v0][i] < INF 表示v0到某点i有直接路径(即边)dist[i] < dist[v0] 表示离目标点越走越近。
#include <iostream> #include <cstring> using namespace std; const int MAXN = 1010; const int INF = 0x7ffffff; int Edge[MAXN][MAXN]; int S[MAXN]; int dist[MAXN]; int mark[MAXN]; int n; void Dijkstra( int v0 ) { int i, j, k; for(i = 0; i < n; ++i) { dist[i] = Edge[v0][i]; S[i] = 0; } S[v0] = 1; dist[v0] = 0; for(i = 0; i < n; ++i) { int min = INF; int u = v0; for(j = 0; j < n; ++j) { if( !S[j] && dist[j] < min) { min = dist[j]; u = j; } } if( min == INF ) break; S[u] = 1; for(k = 0; k < n; ++k) { if( Edge[u][k] < INF && !S[k] && dist[k] > dist[u] + Edge[u][k]) dist[k] = dist[u] + Edge[u][k]; } } } int DFS( int v0 ) { int i; if( v0 == 1 ) return 1; if( mark[v0] ) return mark[v0]; for(i = 0; i < n; ++i) { if( Edge[v0][i] < INF && dist[v0] > dist[i]) mark[v0] += DFS( i ); } return mark[v0]; } int main() { int i, j; int m; int a, b, c; while( cin>>n && n) { cin>>m; memset( mark, 0, sizeof( mark)); for(i = 0; i < n; ++i) for(j = 0; j < n; ++j) Edge[i][j] = INF; for(i = 0; i < m; ++i) { cin>>a>>b>>c; Edge[ a-1 ][ b-1 ] = Edge[ b-1 ][ a-1 ] = c; } Dijkstra( 1 ); cout<<DFS( 0 )<<endl; } return 0; }
相关文章推荐
- HDU-1142 A Walk Through the Forest(最短路径+记忆化搜索)
- HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】
- hdu 1142 A Walk Through the Forest
- hdu1142-A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
- A Walk Through the Forest HDU 1142
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest<java>
- Hdu-1142 A Walk Through the Forest
- HDOJ/HDU 1142 A Walk Through the Forest
- HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)
- HDU 1142 A Walk Through the Forest (dijkstra+记忆化搜索)
- HDU 1142 A Walk Through the Forest (SPFA+记忆化搜索)
- HDU 1142 A Walk Through the Forest
- 【解题报告】HDU -1142 A Walk Through the Forest
- 【Dijkstra+DFS(记忆化)】hdu 1142 A Walk Through the Forest(外:hdu 1428)
- hdu1142 A Walk Through the Forest