A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7995    Accepted Submission(s): 2943

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.
 
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 6
1 3 2
1 4 2
3 4 3
1 5 12
4 2 34
5 2 24
7 8
1 3 1
1 4 1
3 7 1
7 4 1
7 5 1
6 7 1
5 2 1
6 2 1
0
 
Sample Output
2
4
 
Source
 
题意:
从起点1到终点2,求这样的路的条数:如果从a点到2的距离大于从b点到2的距离,并且a能到b那么就从a走到b。问这样的路有几条。
代码:
 //并不是求最短路的条数。先dijk出2到每个点的最短路,在记忆化搜索出路径数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAX=;
int mp[][],dis[],vis[],path[];
int n,m;
void dijk()
{
for(int i=;i<=n;i++)
{
dis[i]=mp[][i];
vis[i]=;
}
vis[]=;
for(int i=;i<=n;i++)
{
int Min=MAX,sta=; //sta初始化一下,防止下面找不到j,而出现vis[sta]数组越界。
for(int j=;j<=n;j++)
{
if(!vis[j]&&dis[j]<Min)
{
Min=dis[j];
sta=j;
}
}
vis[sta]=;
for(int j=;j<=n;j++)
{
if(!vis[j]&&mp[sta][j]!=MAX&&dis[j]>dis[sta]+mp[sta][j])
dis[j]=dis[sta]+mp[sta][j];
}
}
}
int dfs(int x)
{
if(path[x]!=-) //记忆化
return path[x];
if(x==)
return ;
path[x]=;
for(int i=;i<=n;i++)
{
if(mp[x][i]!=MAX&&dis[x]>dis[i])
path[x]+=dfs(i);
}
return path[x];
}
int main()
{
int a,b,c;
while(scanf("%d",&n)&&n)
{
scanf("%d",&m);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
mp[i][j]=i==j?:MAX;
for(int i=;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
mp[a][b]=mp[b][a]=min(mp[a][b],c);
}
dijk();
memset(path,-,sizeof(path));
int ans=dfs();
printf("%d\n",ans);
}
return ;
}
代码:
 

*HDU1142 最短路+记忆化dfs的更多相关文章

  1. hdu 1142 最短路+记忆化

    最短路+记忆化搜索HDU 1142 A Walk Through the Forest链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 > 题意 ...

  2. 从DFS到记忆化DFS到动态规划

    什么是动态规划? 动态规划(Dynamic Programming)是通过组合子问题的解来解决问题的.动态规划是用于求解包含重叠子问题的最优化问题的方法.其基本思想是,将原问题分解为相似的子问题.在求 ...

  3. 【poj3252】 Round Numbers (数位DP+记忆化DFS)

    题目大意:给你一个区间$[l,r]$,求在该区间内有多少整数在二进制下$0$的数量$≥1$的数量.数据范围$1≤l,r≤2*10^{9}$. 第一次用记忆化dfs写数位dp,感觉神清气爽~(原谅我这个 ...

  4. HDU1978How Many Ways 记忆化dfs+dp

    /*记忆化dfs+dp dp[i][j]代表达到这个点的所有路的条数,那么所有到达终点的路的总数就是这dp[1][1]加上所有他所能到达的点的 所有路的总数 */ #include<stdio. ...

  5. 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919

    大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...

  6. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  7. HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  8. HDU1142 (Dijkstra+记忆化搜索)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  9. 洛谷 P3953 逛公园【spfa+记忆化dfs+bfs】

    spfa预处理出最短路数组dis,然后反向建边bfs出ok[u]表示u能到n点 然后发现有0环的话时候有inf解的,先dfs找0环判断即可 然后dfs,设状态f[u][v]为到u点,还可以跑最短路+v ...

随机推荐

  1. 万恶的jar包冲突

    搭了个spring+struts2+mybatis的项目架子, 好久不用myEclipse和tomcat了,生疏了好多. 建议还是去百度一些框架整合的博客,直接使用博客里面给的jar包列表里的jar包 ...

  2. myeclipse 第一步

    今天查myeclipse的一个不常用快捷键的时候,意外发现了一个学习myeclipse开端的博文(别问我是怎么查到这篇博文的....两个字:顺手),感觉比较详细,特意记下来, 别人的分享博客: htt ...

  3. Go - 变量初始化 及 注意事项

    Go变量 初始化 对 复合类型(数组.切片.字典.结构体)变量的初始化是,有一些语法限制: 1.初始化表达式必须包含类型标签: 2.左花括号必须在类型尾部,不能另起一行: 3.多个成员初始值以逗号分隔 ...

  4. java基础 字符串 “==” 和 “equals” 比较

    demo: public class TestStringEquals { public static void main(String[] args) { String a = "test ...

  5. UIStackView

    既要温故又要知新...温故而知新,我懂. 在Cocoachina上看到了,UIStackView. 没用过额,试iOS9的新特性.一研究,妈的还挺眼熟.一想,这不和Android上的线性布局一样么.. ...

  6. Android模拟位置信息

    Android模拟位置程序,俗称GPS欺骗,只能修改采用GPS定位的软件. 手机定位方式目前有4种:基站定位,WIFI定位,GPS定位,AGPS定位 常见的修改手法: 1. 抓包欺骗法,抓包改包欺骗服 ...

  7. mongoosejs学习地址

    http://mongoosejs.com/docs/api.html#querystream-js Node的小基友supervisor 和 forever 不要忘记了,相信你会喜欢他们的:) ht ...

  8. Nginx与Apache的比较

    Nginx与Apache的比较 Nginx相对于Apache的优点 轻量级.同样起web服务,比apache占用更少的资源和内存 抗并发.nginx处理请求是异步非阻塞,而apache则是阻塞型.在高 ...

  9. WebConfig配置

    安全配置示例 https://msdn.microsoft.com/zh-cn/magazine/gg309184.aspx

  10. ytu 1050:写一个函数,使给定的一个二维数组(3×3)转置,即行列互换(水题)

    1050: 写一个函数,使给定的一个二维数组(3×3)转置,即行列互换 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 112[ ...