hdu 1142 A Walk Through the Forest bfs+记忆化搜索
来源:互联网 发布:红蜘蛛教学软件介绍 编辑:程序博客网 时间:2024/03/28 17:42
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8705 Accepted Submission(s): 3221
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题意:J老师的家在一片森林的一端,而他的工作地点则在另一端,最近他感到工作的压力很大,所以打算每天走路回家放松自己,因为路上可以在森林中看到很多美丽的景色,森林里有很多路口,所以他有很多中回家的路可以选择,但是他当然不想朝着离家越来越远的路走,如果一个路口距离家的距离中有比当前路口到家的所有路都要近的话,那么走到那个路口就是朝着离家近的方向走,J老师想知道他可以有多少中不同的回家的路。
Input
输入包括多组数据,以一个0结束,J老师将所有的路口或者路从1开始标号,他的工作地点标为1,家标为2,每组数据第一行给出路口数量N,1<N<=1000,然后是路的数量M,然后为M行每行三个整数a,b以及d,1<=d<=1000000,d表示从路口 a到路口b的距离,没两个路口间最多有一条路。
思路:这个题比较简单,先用bfs求出其他点到2的最短距离,然后在dfs记忆化搜。#include<stdio.h>#include<string.h>#include<stdlib.h>#include<vector>#include<queue>using namespace std;#define INF 999999999;struct edge{ int from,to; int w;};int n,m;vector<int>G[1001];vector<edge>edges;int dis[1001];int d[1001];void addedge(int x,int y,int w){ edge a={x,y,w}; edges.push_back(a); edge b={y,x,w}; edges.push_back(b); G[x].push_back(edges.size()-2); G[y].push_back(edges.size()-1);}void bfs()//bfs求最短路{ for(int i=1;i<=n;i++) { dis[i]=INF; } queue<int>q; q.push(2); dis[2]=0; while(!q.empty()) { int a=q.front(); q.pop(); for(int i=0;i<G[a].size();i++) { edge b=edges[G[a][i]]; if(dis[b.to]>dis[a]+b.w) { dis[b.to]=dis[a]+b.w; q.push(b.to); } } }}int dfs(int x)//记忆化求路的条数{ if(d[x]!=-1) return d[x]; int count=0; for(int i=0;i<G[x].size();i++) { edge a=edges[G[x][i]]; if(dis[a.to]<dis[x]) { count+=dfs(a.to); } } d[x]=count; return d[x];}int main(){ while(scanf("%d",&n)&&n) { scanf("%d",&m); for(int i=1;i<=n;i++) G[i].clear(); edges.clear(); for(int i=1;i<=m;i++) { int x,y,w; scanf("%d %d %d",&x,&y,&w); addedge(x,y,w); } bfs(); /*for(int i=1;i<=n;i++) { printf("%d ",dis[i]); } printf("\n");*/ memset(d,-1,sizeof(d)); d[2]=1; dfs(1); printf("%d\n",d[1]); }}
阅读全文
0 0
- hdu 1142 A Walk Through the Forest bfs+记忆化搜索
- 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 (SPFA+记忆化搜索)
- 【HDU】1142 A Walk Through the Forest 最短路+记忆化搜索
- HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索
- hdu 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索)
- HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)
- 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 (digkstra+记忆化搜索)
- hdu 1142/hrbust 1347 A Walk Through the Forest/森林中的路【SPFA+记忆化搜索】
- HDU 1142 A Walk Through the Forest (记忆化搜索+Dijkstra算法)
- HDU 1142 A Walk Through the Forest (dijkstra+记忆化搜索)
- HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】
- HDU 1142 A Walk Through the Forest (dijkstra+记忆化搜索)
- 内存管理——Xcode升级到8.0之后内存管理需要注意的事项
- JavaScript core笔记
- C语言 堆栈
- 链表反转
- Java学习笔记 03
- hdu 1142 A Walk Through the Forest bfs+记忆化搜索
- Python 学习资源大全中文版
- znode节点创建
- BOOST 类型转换(lexical_cast)崩溃问题
- 安装Laravel
- Java 泛型详解
- 数组中指针的指针访问
- 测试
- 博客大咖收藏录