A Walk Through the Forest (最短路+记忆化搜索)
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 (最短路+记忆化搜索)的更多相关文章
- HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
- UVa10917 A Walk Through the Forest(SPFA+记忆化搜索)
题目给一张有向图,问从起点1到终点2沿着合法的路走有种走法,合法的路指从u到v的路,v到终点的距离严格小于u到终点的距离. 先SPFA预处理出所有合法的路,然后这些路肯定形成一个DAG,然后DP一下就 ...
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
- 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 ...
- Luogu P3953 逛公园(最短路+记忆化搜索)
P3953 逛公园 题面 题目描述 策策同学特别喜欢逛公园.公园可以看成一张 \(N\) 个点 \(M\) 条边构成的有向图,且没有自环和重边.其中 \(1\) 号点是公园的入口,\(N\) 号点是公 ...
- hdu 1428(很好的一道题,最短路+记忆化搜索)
漫步校园 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Luogu P2149 [SDOI2009]Elaxia的路线(最短路+记忆化搜索)
P2149 [SDOI2009]Elaxia的路线 题意 题目描述 最近,\(Elaxia\)和\(w**\)的关系特别好,他们很想整天在一起,但是大学的学习太紧张了,他们必须合理地安排两个人在一起的 ...
- hdu 1142 最短路+记忆化
最短路+记忆化搜索HDU 1142 A Walk Through the Forest链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 > 题意 ...
- hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
随机推荐
- 实验09——java基于TCP实现客户端与服务端通信
TCP通信 需要先创建连接 - 并且在创建连接的过程中 需要经过三次握手 底层通过 流 发送数据 数据没有大小限制 可靠的传输机制 - 丢包重发 包的顺序的 ...
- Linux学习笔记之linux的文件目录结构
Linux环境下,一切皆文件! linux和windows系统有区别, windows是在各个硬盘上进行分区,分区里面又有好多文件, 而linux是采用树状的目录结构,所有都在根目录 / 下,所有 ...
- GitLab 数据库
访问 GitLab 数据库 步骤 用的 Docker Gitlab,首先进入容器 docker exec -it gitlab /bin/bash `` 找到数据库配置文件 ```bash /var/ ...
- 安装mpi的那些坑
安装mpi可以用 yum 安装 具体从操作步骤可参考这个链接mpi的yum安装方式 mpi在linux下 使用root权限 会报错 修改方式有两种 1 2.换其他账户 mpi会自行评估我们cpu的 ...
- 尝试Access数据库注入实验
靶场环境:https://www.mozhe.cn/bug/detail/82 首先http://219.153.49.228:49543/new_list.asp?id=1 order by 4 到 ...
- java 判断集合元素唯一的原理
一 ArrayList的contains方法判断元素是否重复原理 ArrayList的contains方法会使用调用方法时,传入的元素的equals方法依次与集合中的旧元素 所比较,从而根据返回的布尔 ...
- ReentrantLock与synchronized 源码解析
一.概念及执行原理 在 JDK 1.5 之前共享对象的协调机制只有 synchronized 和 volatile,在 JDK 1.5 中增加了新的机制 ReentrantLock,该机制的诞生并 ...
- Springboot调用Oracle存储过程的几种方式
因工作需要将公司SSH项目改为Spingboot项目,将项目中部分需要调用存储过程的部分用entityManagerFactory.unwrap(SessionFactory.class).openS ...
- python库安装失败的解决方法
安装python库 在https://www.lfd.uci.edu/~gohlke/pythonlibs 中,搜索对应库名称 选取对应版本下载 在cmd窗口中,用命令 pip install+文件路 ...
- 【期外】(三)Linux下集成开发环境Geany
今天小编发现了一个很好的软件,它的名字就叫做Geany. 这是Linux系统中的开发工具,相当的好用. Linux与windows最大的不同正是不是集成开发环境,所以写起代码来总是用文档写好后,然后再 ...