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. 

InputInput 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. 
OutputFor 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 代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define Inf 0x3f3f3f3f const int maxn=1e5+;
typedef long long ll;
using namespace std;
int n,m;
vector<int>G[];
int Map[][];
int dis[];
int s[];
void init()
{
for(int t=;t<=n;t++)
{
for(int j=;j<=n;j++)
{
Map[t][j]=Inf;
}
}
for(int t=;t<=n;t++)
{
Map[t][t]=;
}
for(int t=;t<=n;t++)
{
G[t].clear();
}
memset(s,,sizeof(s));
}
void Dijstra(int u)
{
for(int t=;t<=n;t++)
{
dis[t]=Inf;
}
priority_queue<int,vector<int>,greater<int> >q;
q.push(u);
dis[u]=;
int now;
while(!q.empty())
{
now=q.top();
q.pop();
for(int t=;t<G[now].size();t++)
{
if(Map[now][G[now][t]]+dis[now]<dis[G[now][t]])
{
dis[G[now][t]]=Map[now][G[now][t]]+dis[now];
q.push(G[now][t]);
}
}
}
}
int dfs(int now)
{ if(now == ) return ;
if(s[now]) return s[now];
for(int i = ; i < G[now].size(); i++)
{
if(dis[now] > dis[ G[now][i] ])
{
s[now] += dfs(G[now][i]);
}
}
return s[now]; } int main()
{ while(scanf("%d",&n)!=EOF)
{
if(n==)
{
break;
}
scanf("%d",&m);
int u,v,w;
init();
for(int t=;t<m;t++)
{
scanf("%d%d%d",&u,&v,&w);
if(Map[u][v]>w||Map[v][u]>w)
{
Map[u][v]=w;
Map[v][u]=w;
G[u].push_back(v);
G[v].push_back(u);
}
}
Dijstra();
printf("%d\n",dfs());
} return ;
}

A Walk Through the Forest (最短路+记忆化搜索)的更多相关文章

  1. HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)

    题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...

  2. UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)

    题目给一张有向图,问从起点1到终点2沿着合法的路走有种走法,合法的路指从u到v的路,v到终点的距离严格小于u到终点的距离. 先SPFA预处理出所有合法的路,然后这些路肯定形成一个DAG,然后DP一下就 ...

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

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

  4. 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 ...

  5. Luogu P3953 逛公园(最短路+记忆化搜索)

    P3953 逛公园 题面 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 \(N\) 个点 \(M\) 条边构成的有向图,且没有自环和重边.其中 \(1\) 号点是公园的入口,\(N\) 号点是公 ...

  6. hdu 1428(很好的一道题,最短路+记忆化搜索)

    漫步校园 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. Luogu P2149 [SDOI2009]Elaxia的路线(最短路+记忆化搜索)

    P2149 [SDOI2009]Elaxia的路线 题意 题目描述 最近,\(Elaxia\)和\(w**\)的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们必须合理地安排两个人在一起的 ...

  8. hdu 1142 最短路+记忆化

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

  9. hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)

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

随机推荐

  1. excel-删除

    问题[1]:删除不整齐数据. 1 编号 单词本身 词性 命名实体 依存句法父节点 依存句法 谓词 语义角色 2 0 < wp O 1 WP _ - 3 1 弄臣 n O -1 HED _ - 4 ...

  2. 小伙子自从学会用Python爬取岛国“动作”电影,身体一天不如一天

    在互联网的世界里,正确的使用VPN看看外面的世界,多了解了解世界的发展.肉身翻墙后,感受一下外面的肮脏世界.墙内的朋友叫苦不迭,由于某些原因,VPN能用的越来越少.上周我的好朋友狗子和我哭诉说自己常用 ...

  3. office2010的破解工具

    office2010的破解工具,找了好多的密钥都不合适,直接用这个软件一键搞定, 下载地址:https://pan.baidu.com/s/1phPwihCDipGwGdSmjWNeYw 提取码:8m ...

  4. Linux学习笔记之如何在图形界面旁边把终端添加显示出来

    首先旁边无终端,我们可以点击ctrl+alt+t,可以把终端显示出来 右键点击终端,然后点击Lock to Launcher,然后完成 PS:不想显示也可以点击其右键,选择Unlock from La ...

  5. PXE安装与配置

    PXE 安装与配置 实验环境 VMware Fusion 虚拟机 node1有两块网卡, ens33(172.100.16.10)-->bridge, ens37-->vmnet4(192 ...

  6. 你可以 CRUD,但你不是 CRUD 程序员!

    什么是务实 务实程序员他们总是在面临问题时,透过问题看到本质,从具体的场景出发,从大局着想,了解整个问题的来龙去脉,他们会对自己的行为负责,在项目面临问题时,他们不会撒手不管或者任由风险一步步扩大直至 ...

  7. 高级搜索树-AVL树

    目录 平衡因子 AVL树节点和AVL树的定义 失衡调整 插入和删除操作 完整源码 AVL树是平衡二叉搜索树中的一种,在渐进意义下,AVL树可以将高度始终控制在O(log n) 以内,以保证每次查找.插 ...

  8. C#LeetCode刷题之#9-回文数(Palindrome Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3840 访问. 判断一个整数是否是回文数.回文数是指正序(从左向右 ...

  9. sourcetree关于注册的问题

    当前只有Win的版本,Mac自行百度(笑) 很多人用git命令行不熟练,那么可以尝试使用sourcetree进行操作. 然鹅~~sourcetree又一个比较严肃的问题就是,很多人不会跳过注册或者操作 ...

  10. Core + Vue 后台管理基础框架9——统一日志

    1.背景 前阵子有园友留言,提到日志相关的东西,同时,最近圈子里也有提到日志这个东西.一个充分.集中的统一日志平台还是很有必要的,否则系统出问题了只能靠猜或者干瞪眼.何谓充分,日志记录满足最低要求.出 ...