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 ...
随机推荐
- 面试被问:如果系统 CPU 突然飙升且 GC 频繁,你该如何排查?
出自:开源中国 原文:系统运行缓慢,CPU 100%,以及Full GC次数过多问题的排查思路 处理过线上问题的同学基本上都会遇到系统突然运行缓慢,CPU 100%,以及Full GC次数过多的问题. ...
- Python自动化办公知识点整理汇总
知乎上有人提问:用python进行办公自动化都需要学习什么知识呢? 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却 ...
- JS 执行机制笔记
js同步和异步同步 前一个任务结束以后再执行下面一个任务,程序的执行顺序与任务的排列顺序是一致的 同步任务都在主线程上执行,形成一个执行线 异步 前一个任务没结束之前程序还可以执行别的任务 j ...
- 程序员必须了解!IntelliJ IDEA 2020.2的新增功能
IDEA 因为之前破解过,所以家里的电脑都是19版本的,用的也比较顺手,也就一直懒得去动他,但是,程序猿的好奇心可能真的挺重,猎奇心里,在网上也看到了很多关于2020版本的idea的各种好处,于是,闲 ...
- 93复原IP地址。
from typing import List# 这道题不是很难,但是限制条件有很多.# 用递归的方法可以很容易的想到.只需要四层递归就好了.# 每次进行加上限制条件.过滤每一层就好了..class ...
- C#设计模式之2-抽象工厂模式
抽象工厂模式(Abstract Factory Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/391 ...
- java实现一个简单的爬虫小程序
前言 前些天无意间在百度搜索了一下以前写过的博客 我啥时候在这么多不知名的网站上发表博客了???点进去一看, 内容一模一样,作者却不是我... 然后又去搜了其他篇博客,果然,基本上每篇都在别的网站上有 ...
- 深入解析Laravel的中间件
Laravel 中间件是什么? 简而言之,中间件在 laravel 中的作用就是过滤 HTTP 请求,根据不同的请求来执行不同的逻辑操作. 我们可以通过中间件实现以下功能: 指定某些路由 设置 HTT ...
- 蒲公英 · JELLY技术周刊 Vol.17: 90 行代码实现 React Hooks
蒲公英 · JELLY技术周刊 Vol.17 React Hooks 相信大家都不陌生,自被设计出以来就备受好评,在很多场景中都有极高的使用率,其中原理更是很多大厂面试中的必考题,很多朋友都能够如数家 ...
- C# NPOI计算Execl里面的公式
我这里分两种情况处理 1.Execl中表格中存在公式,我们通过公式获取数据 我们通过Npoi,获取列的属性: private static object GetValueType(ICell cell ...