hdu 1142 A Walk Through the Forest (最短路 + 记忆化搜索)
来源:互联网 发布:windows驱动开发是什么 编辑:程序博客网 时间:2024/04/28 18:36
读题不仔细,一直以为是求长度最短的路由多少条!!
/*“ 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. ”A点能走到B点的条件,是A与B相连,并且A到终点的距离要>B点到终点的距离。所以把终点2当成起点,求出所有点到2的距离。然后DFS出有多少条满足条件的路径 ,记忆化搜索*/
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define M 1000000000int dis[1001],map[1001][1001],vis[1001],n,m,a,b,d,ans,p,h,fa[1001],num[1001];int dfs(int i){ if(i==2) return 1; else if(num[i]) return num[i]; int sum=0; for(int j=1;j<=n;j++) { if(map[i][j]!=M&&dis[i]>dis[j]) { if(num[j]) sum+=num[j]; else sum+=dfs(j); } } num[i]=sum+num[i]; return num[i];}int main(){ while(scanf("%d",&n)) { if(n==0) break; scanf("%d",&m); int i,j; for(i=1;i<=n;i++) for(j=1;j<=n;j++) map[i][j]=M; for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&d); if(map[a][b]>=d) { map[a][b]=d; map[b][a]=d; } } memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++) dis[i]=M; dis[2]=0; for(i=1;i<=n;i++) { int x,mm=M; for(j=1;j<=n;j++) { if(!vis[j]&&dis[j]<=mm) { x=j; mm=dis[j]; } } vis[x]=1; for(j=1;j<=n;j++) dis[j]=min(dis[j],map[x][j]+dis[x]); } memset(num,0,sizeof(num)); printf("%d\n",dfs(1)); } return 0;}
- 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-最短路+记忆化搜索
- HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)
- HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】
- HDU 1142 A Walk Through the Forest 最短路+记忆化搜索
- hdu 1142 A Walk Through the Forest 最短路+记忆化搜索
- 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 最短路+记忆搜索
- HDU-1142 A Walk Through the Forest(最短路径+记忆化搜索)
- POJ 2662-A Walk Through the Forest(最短路+记忆化搜索)
- HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索
- A Walk Through the Forest 最短路+记忆化搜索DFS
- WAVELETS/WTpart3
- 记录一些比较有用的网站
- C/C++内存管理详解
- PHP定时执行任务的实现
- 不顾惜朝终身误,一顾惜朝误终身。
- hdu 1142 A Walk Through the Forest (最短路 + 记忆化搜索)
- Delphi纯真IP数据库查找[源码]
- Google Code 项目代码托管网站上 Git 版本控制系统使用简明教程
- apache Maven 入门
- Sublime Text 2
- [AIR iOS] ANE 收集
- iphone经纬度计算距离与检测瞬时速度/最后的英文问答部分是关键
- Sublime Text 2 入门及技巧
- android系统中,AIR开发APK,如何屏蔽返回键