HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】
来源:互联网 发布:苹果mac版软件 编辑:程序博客网 时间:2024/05/17 04:40
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7601 Accepted Submission(s): 2796
Problem Description
Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.
Input
Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths starting with 1. His office is numbered 1, and his house is numbered 2. The first line of each test case gives the number of intersections N, 1 < N ≤ 1000, and the number of paths M. The following M lines each contain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000 indicating a path of length d between intersection a and a different intersection b. Jimmy may walk a path any direction he chooses. There is at most one path between any pair of intersections.
Output
For each test case, output a single integer indicating the number of different routes through the forest. You may assume that this number does not exceed 2147483647
Sample Input
5 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10
Sample Output
24
#include <iostream>#include <cstring>#include<stdio.h>//#define INT 1000000000+5 如果 2*INT 2*1000000000+5则值是2000000005using namespace std;const int INT=0x3f3f3f3f;//定义常量,最好用const ,本题必须用16进制 用10进制表示INT 会出错 我也不知道为什么const int maxn=1005;int dist[maxn];int map[maxn][maxn];bool vis[maxn];int dp[maxn];int dian,side;void Dijkstra(int x){ for(int i=1;i<=dian;i++) { dist[i]=map[x][i];//点2到各个点的距离记录到dist[i]中 } memset(vis,false,sizeof(vis));//标记所有是没走过 vis[x]=true; dist[x]=0;//一定要确定这一步 int index; for(int i=1;i<=dian;i++) { int minn=INT; for(int ii=1;ii<=dian;ii++)//遍历所有点,找出第一个到2的最小距路径 标记给index { if(!vis[ii]&&dist[ii]<minn)//也就是所有2能到达的点中,index到2的距离最短 { minn=dist[ii]; index=ii; } } if(minn==INT) break;//如果所有的点都无法到达 2 退出循环 vis[index]=true; for(int j=1;j<=dian;j++)//更新一遍所有的点到2的距离 { if(!vis[j]&&dist[index]+map[index][j]<dist[j])//没标记过 && 符合题意的条件 { dist[j]=dist[index]+map[index][j]; } } }}int DFS(int x){ int sum=0; if(dp[x]) return dp[x]; for(int i=1;i<=dian;i++) { if(map[x][i]!=INT&&dist[x]>dist[i])//(每次搜索一条能走的路&&下一个点i到2的距离要小于这个点到2的距离) { //printf("i=%d \n",i); sum+=DFS(i); //printf("%d\n",i); } } return dp[x]=sum;}int main (void){ int a,b; int far; while(~scanf("%d",&dian),dian) { scanf("%d",&side); memset(map,INT,sizeof(map)); for(int i=1; i<=side; i++) { scanf("%d%d%d",&a,&b,&far); map[a][b]=map[b][a]=far; } Dijkstra(2); /* for(int i=1;i<=dian;i++) { printf("i=%d dist=%d\n",i,dist[i]); }*/ memset(dp,0,sizeof(dp)); dp[2]=1; printf("%d\n",DFS(1)); } return 0;}
0 0
- HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】
- hdu 1142 A Walk Through the Forest (最短路 + 记忆化搜索)
- hdu 1142 A Walk Through the Forest(spfa求最短路+记忆化搜索)
- 【HDU】1142 A Walk Through the Forest 最短路+记忆化搜索
- hdu-1142-A Walk Through the Forest-最短路+记忆化搜索
- HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)
- 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 最短路+记忆化搜索 解题报告
- HDU 1142 A Walk Through the Forest (记忆化搜索+Dijkstra算法)
- hdu 1142 A Walk Through the Forest Dijkstra+记忆搜索
- HDU 1142 A Walk Through the Forest 最短路+记忆搜索
- POJ 1142 A Walk Through the Forest ( 最短路 +记忆化搜索)
- hdoj 1142 A Walk Through the Forest(记忆化搜索+最短路)
- HDOJ 1142 A Walk Through the Forest(最短路+记忆化搜索)
- hdu 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索)
- HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)
- 制作带三角指向的overlay/popup
- 大型网站系统架构演化之路
- web.py– sessions
- failed to find Build Tools revision错误的解决
- Thinking In Java的学习之路 —— Java定制工具库
- HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】
- 最简单的Dagger2入门教程
- 25. Reverse Nodes in k-Group
- Swift中文教程(十八) 类型检查
- WEB-INF目录与META-INF目录的作用
- Android程序与服务器交互(一)
- CATransform3D 特效详解
- 学习资料相关网站
- Swift中文教程(十九) 类型嵌套