hdu 1142 A Walk Through the Forest (最短路+dfs)
2012-03-21 16:03
531 查看
先求一遍最短路,再根据求出来的每一个点到达终点的最短距离,dfs计数
View Code
View Code
#include<iostream> #include<algorithm> using namespace std; const int N =1001; int g ,n,m,dist ; int ans,dp ; bool vis ; void init() { for(int i=0;i<=n;i++) for(int j=0;j<=n;j++) g[i][j]=INT_MAX; } void Dijkstra(int s) { memset(vis,false,sizeof(vis)); vis[s]=true; for(int i=1;i<=n;i++) dist[i]=g[s][i]; dist[s]=0; for(int i=1;i<n;i++) { int mind=INT_MAX,v=0; for(int j=1;j<=n;j++) { if(!vis[j] && dist[j]<mind) { mind=dist[j]; v=j; } } vis[v]=true; for(int k=1;k<=n;k++) { if(g[v][k]!=INT_MAX && !vis[k]) { int newdis=dist[v]+g[v][k]; if(newdis<dist[k]) dist[k]=newdis; } } } } int dfs(int u) { if(u==2) { return dp[u]=1; } if(dp[u]!=-1) return dp[u]; int sum=0; for(int i=1;i<=n;i++) { if(g[u][i]!=INT_MAX && dist[u]>dist[i]) sum+=dfs(i); } return dp[u]=sum; } int main() { int a,b,c; while(scanf("%d",&n)==1&&n) { init(); scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&c); g[a][b]=g[b][a]=c; } Dijkstra(2); memset(dp,-1,sizeof(dp)); printf("%d\n",dfs(1)); } return 0; }
相关文章推荐
- hdu 1142 A Walk Through the Forest (最短路+dfs )
- 【最短路+dfs+dijkstra】杭电 hdu 1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest【Dijkstra+dfs求最短路条数】
- hdu 1142 A Walk Through the Forest(最短路+记忆化搜索)
- ACM HDU 1142 A Walk Through the Forest(单源最短路,记忆DFS)
- 杭电1142 A Walk Through the Forest (最短路+DFS+dp)
- HDU - 1142 A Walk Through the Forest (DP + 最短路)
- hdu 1142 A Walk Through the Forest(最短路径+dfs)
- HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)
- HDU 1142 A Walk Through the Forest(最短路+dfs搜索)
- hdu 1142 A Walk Through the Forest(spfa求最短路+记忆化搜索)
- 【HDU】1142 A Walk Through the Forest 最短路+记忆化搜索
- HDU 1142 A Walk Through the Forest【最短路 + dfs记忆化搜索】
- HDU 1142 A Walk Through the Forest 最短路+记忆搜索
- HDU 1142 A Walk Through the Forest (SPFA+记忆化DFS)
- hdu1142-A Walk Through the Forest-最短路+dfs
- HDU 1142 A Walk Through the Forest (求最短路条数)
- HDU 1142 A Walk Through the Forest (记忆化搜索 最短路)
- hdu1142 A Walk Through the Forest 最短路和记忆式搜索dijstra+dfs
- hdu 1142 A Walk Through the Forest(最短路)