HDU1142-A Walk Through the Forest(记忆化搜索+SPFA)
来源:互联网 发布:男性卫生知况 编辑:程序博客网 时间:2024/05/16 04:10
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7832 Accepted Submission(s): 2895
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
Source
University of Waterloo Local Contest 2005.09.24
#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#include <iostream>using namespace std;int map[1009][1009];const int INF=0x3f3f3f3f;int dis[1009];int visit[1009];int sum[1009],n;void SPFA(){ queue<int>q; memset(visit,0,sizeof visit); visit[2]=1; q.push(2); while(!q.empty()) { int t=q.front(); visit[t]=0; q.pop(); for(int i=1;i<=n;i++) { if(dis[i]>map[t][i]+dis[t]) { dis[i]=map[t][i]+dis[t]; if(!visit[i]) visit[i]=1,q.push(i); } } }}int dfs(int s){ if(sum[s]) return sum[s]; if(s==2) return 1; int ans=0; for(int i=1;i<=n;i++) { if(map[s][i]!=INF&&dis[s]>dis[i]) ans+=dfs(i); } sum[s]=ans; return sum[s];}int main(){ int m; while(~scanf("%d",&n)) { if(!n) break; scanf("%d",&m); for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } for(int i=0;i<=n;i++) dis[i]=INF; dis[2]=0; int a,b,c; for(int i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&c); if(map[a][b]>c) map[a][b]=map[b][a]=c; } SPFA(); memset(sum,0,sizeof sum); printf("%d\n",dfs(1)); } return 0;}
0 0
- HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】
- hdu1142 - A Walk Through the Forest(SPFA+记忆化搜索)
- HDU1142——A Walk Through the Forest(spfa+记忆化搜索)
- HDU1142-A Walk Through the Forest(记忆化搜索+SPFA)
- hdu1142 A Walk Through the Forest(spfa+记忆化搜索)
- HDU1142 A Walk Through the Forest(SPFA+记忆化搜索)
- hdu1142 A Walk Through the Forest —— dijkstra + 记忆化搜索
- hdu1142 A Walk Through the Forest( Dijkstra算法+搜索)
- hdu 1142 A Walk Through the Forest(spfa求最短路+记忆化搜索)
- hdu1142 A Walk Through the Forest 最短路和记忆式搜索dijstra+dfs
- HDU 1142 A Walk Through the Forest (SPFA+记忆化搜索)
- Hdoj 1428 A Walk Through the Forest 【spfa】+【记忆化搜索】
- HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)
- hdu 1142/hrbust 1347 A Walk Through the Forest/森林中的路【SPFA+记忆化搜索】
- hdu1142 A Walk Through the Forest
- HDU1142 A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu1142-A Walk Through the Forest
- java 多线程相关问题
- 非阻塞IO
- DOM学习笔记----------(一)
- POJ 1061 青蛙的约会
- AAA Quartz-Spring[一]之MethodInvokingJobDetailFactoryBean配置任务
- HDU1142-A Walk Through the Forest(记忆化搜索+SPFA)
- 计算机科学篇笔记(一)
- Sprin MVC中DispatcherServlet和ContextLoaderListener的关系
- HQL语法
- javax.el.ELResolver错误
- 第一行代码系列第二章——在活动中使用menu以及销毁一个活动
- Java多线程 -- JUC包源码分析13 -- Callable/FutureTask源码分析
- JVM结构、GC工作机制详解
- 给 nanopi m2 Android系统安装busybox