A Walk Through the Forest(hdu1142,最短路+dfs)

Air780E模组AT指令实战:5分钟搞定MQTT连接EMQX服务器(附完整代码) 本文详细介绍了如何使用Air780E 4G模组通过AT指令快速连接MQTT服务器。从硬件接线、网络注册到MQTT连接配置,提供了完整的实战步骤和代码示例,帮助开发者快速实现物联网设备的数据上报与远程通信。 阅读详情

http://acm.hdu.edu.cn/showproblem.php?pid=1142

A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4623    Accepted Submission(s): 1676

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

University of Waterloo Local Contest 2005.09.24

Recommend

Eddy

解析:

题意:

求:A到B的路径有多少条,其中A,B满足,A到2的路径比B到2的路径长。

思路:

1.由于是无向图,用Dijkstra求以2为起点的最短路径

2.用dfs找出符合条件的路径条数

78MS 4196K 1267 B C++

*/


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
const int maxn=1000+10;
const int inf=1<<29;
int d[maxn],p[maxn],vis[maxn];
int n;
int w[maxn][maxn];
void init()
{
  for(int i=1;i<=n;i++)
   for(int j=1;j<=n;j++)
   {
   	if(i==j)
   	w[i][j]=0;
   	else
   	w[i][j]=inf;
   }
memset(p,-1,sizeof(p));
}
void Dijkstra(int s)
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
d[i]=(i==s? 0:inf);
for(int i=1;i<=n;i++)
{
int x,t=inf;
for(int y=1;y<=n;y++)
if(!vis[y]&&d[y]<=t)//要在等级允许范围内进行
t=d[x=y];
vis[x]=1;
for(int y=1;y<=n;y++)//c[y]表示1到y点的最短路径
if(d[y]>d[x]+w[x][y])
d[y]=d[x]+w[x][y];
}
}
int dfs(int x)
{
if(p[x]!=-1)
return p[x];
p[x]=0;
for(int i=1;i<=n;i++)
if(w[i][x]!=inf&&d[x]>d[i])//路径存在且x到2的最短路大于i到2的最短路
p[x]+=dfs(i);
return p[x];
}
int main()
{
int i,j,m,ans;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
init();
scanf("%d",&m);
int u,v,c;
for(i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&c);
w[u][v]=w[v][u]=c;
//printf("%d %d\n",w[u][v],w[v][u]);
}
Dijkstra(2);
//for(i=1;i<=n;i++)
//printf("d[%d]==%d\n",i,d[i]);
p[2]=1;
dfs(1);
printf("%d\n",p[1]);
}
return 0;
}


【STM32项目】基于Stm32c8t6-镭射激光打印机的设计(完整工程资料源码) 随着打印技术的快速发展,镭射打印技术也开始得到越来越广泛的应用。这种技术可以被用于在木材、塑料、皮革、织物等各种材料上进行图形打印,并且通常比传统机器更快、更准确和更灵活。 阅读详情

相关推荐

手把手教你用TL431设计反激电源补偿器(附波特图调试技巧)

本文详细介绍了如何使用TL431设计反激电源补偿器,并提供了波特图调试技巧。通过实战经验分享,帮助工程师快速掌握补偿网络设计、分压电阻计算和稳定性判据,避免常见设计误区,提升电源稳定性与效率。

weixin_29201859的博客 396

hdu 1142 A Walk Through the Forest (最短路+dfs )

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5809    Accepted Submission(s): 2147 Problem Description

Coding A World 975

哨兵1号(Sentinel-1)雷达SAR数据预处理全流程:从下载到精确配准

本文详细介绍了哨兵1号(Sentinel-1)雷达SAR数据从下载到精确配准的全流程预处理方法。内容涵盖SLC与GRD数据选择、ASF平台下载技巧,并重点解析了使用SNAP软件进行几何校正、辐射定标及高精度配准的核心步骤,为InSAR形变监测等应用提供可靠的数据基础。

weixin_29208327的博客 491

HDU - 1142 - A Walk Through the Forest (最短路+dfs)

题意: 终点到B和A有多少条路径,且终点到B的距离小于A #include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; const int inf = 0x3f3f3f3f; int G[maxn][maxn]; int dis[maxn]; bool vis[maxn]; int p[maxn...

刘二狗的博客 273

PostgreSQL+PostGIS空间数据入库实战:从矢量栅格到GeoServer发布全流程

本文详细介绍了将矢量与栅格空间数据导入PostgreSQL并利用PostGIS扩展进行管理的完整流程。内容涵盖环境搭建、使用shp2pgsql和raster2pgsql工具进行数据入库、基础空间查询验证,以及最终通过GeoServer发布为地图服务的实战步骤,为构建空间数据平台提供清晰指引。

weixin_29268267的博客 297

新装Win10/Win11后VMware虚拟机连不上网?手把手教你排查‘网络信息不可用’(从服务检查到网段配置)

本文详细介绍了在Windows 10/11系统上安装VMware Workstation后虚拟机无法联网的排查方法。从检查VMware NAT Service、DHCP Service等关键服务状态,到重置虚拟网络编辑器配置,再到内核设备驱动问题解决,手把手教你解决‘网络信息不可用’问题。特别针对VMCI驱动故障提供了实用解决方案,帮助用户快速恢复虚拟机网络连接。

weixin_28736335的博客 790

A Walk Through the Forest HDU - 1142 最短路+记忆化搜索DFS

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

why932346109的博客 171

详细了解sklearn中的CountVectorizer

CountVectorizer 是 scikit-learn(一个流行的 Python 机器学习库)中的一个文本特征提取类,其核心功能是将一系列原始文本(如句子、文档)转换为基于词频(word count)的数值矩阵。它基于词袋模型(Bag of Words, BoW)思想,忽略文本的语法结构和词序,仅关注词汇在文本中出现的频率,从而将文本转化为机器可理解的数字形式。文本 1:"I love machine learning"

2302_76568160的博客 1130

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

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

Must so 642

AI协议助手:法律文本解析与智能问答技术解析

自然语言处理(NLP)技术在法律文本解析中展现出巨大潜力,通过BERT等预训练模型实现条款分类与语义理解。结合OCR与知识图谱技术,AI协议助手能快速提取合同核心条款,并以问答形式解释法律术语,显著提升商务场景的决策效率。测试数据显示,该系统对NDA协议的识别准确率达92%,响应时间仅1.2秒,特别适用于人力资源竞业协议、云服务SLA评估等场景。随着RAG架构与法律术语向量库的应用,AI+法律正推动合同审核成本下降67%,未来将在跨境合规、智能谈判等方向持续演进。

weixin_29053577的博客 142

第二节:Vben Admin 最新 v5.0 (vben5) + Python Flask 快速入门 - Python Flask 后端开发详解(附源码)

大家好,我是cuiwin!本节内容,我们从零开始开发基于Python(Flask)的后端项目,对接Vue Vben Admin(vben5)。登录mysql数据库执行: 创建数据库。

cuiwin的专栏 2286

HDU 1142 A Walk Through the Forest 最短路+DFS

点击打开链接 A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4152    Accepted Submission(s): 1519 Problem Des

代码改变世界,keep coding。 1051

实干派!网易易盾增强版滑块验证码,全方位讲解识别思路(含源码)

该验证码是异形滑块验证码中的一种,缺口旋转+偏移的处理需要你有很好的图像处理知识,能很好的处理透明通道,同时,该验证码需要你能很好的处理缺口和背景之间匹配的关系,该验证码的处理难点就是两个缺口之间如何找到正确的,两个检测框都有很强的相似度,且加上背景图的干扰,在图像处理和有优化上难度大大增加。

码王吴彦祖的博客 2032

HDU 1142 A Walk Through the Forest【最短路 + dfs记忆化搜索】

传送门 // 题意: 首先给定一幅无向图(n, m), 起点是1, 终点是2, 图中两点A, B能相互到达的条件是A到终点的距离比B到终点的距离要长. 问1到2有多少种方式. // 思路: 先求出2到所有点的最短路, 然后深搜答案, dfs的过程中保存每个点的信息, 如果再次遇到某个点时他已经有路径条数的信息, 则可以直接放回答案了. AC Code const int maxn =

Anxdada -- 我等风来也等你 574

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【最短路+dfs+dijkstra】杭电 hdu 1142 A Walk Through the Forest

/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2011 panyanyany All rights reserved. URL : http://acm.hdu.edu.c

panyanyany(飞雨幻沙) 1477

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

hdu1142-A Walk Through the Forest-最短路+dfs

下面是spfa+dfs //5601720 2012-03-21 20:40:48 Accepted 1142 31MS 624K 1382 B C++ zjwzcnjsy #include #include #include using namespace std; #define inf 0x3fffffff #define M 1001 struct son{ int v

622

【hdu 1142】A Walk Through the Forest(最短路+DFS)

A Walk Through the ForestTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9950    Accepted Submission(s): 3625Problem DescriptionJimmy experience...

duanghaha的博客 417

hdu1142A Walk Through the Forest (最短路+BFS+DFS )

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 n

青山绿水之辈 专栏 1210

hdu1142 A Walk Through the Forest 最短路和记忆式搜索dijstra+dfs

题目大意:说的是某某从公司回家希望尽可能的走最短的路程。并且

ccsu_cherish 791

hdu 1142 A Walk Through the Forest (最短路+dfs)

先求一遍最短路,再根据求出来的每一个点到达终点的最短距离,dfs计数 View Code #include<iostream>#include<algorithm>using namespace std;const int N =1001;int g[N][N],n,m,dist[N];int ans,dp[N];bool vis[N];void init(){ ...

weixin_30530939的博客 99

A Walk Through the Forest(HDU 1142)---多源单汇最短路问题+DFS路径数

题目链接 题目描述 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, hi...

weixin_43960284的博客 185

hdu 1142 A Walk Through the Forest(最短路+记忆化搜索dfs)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2183    Accepted Submission(s): 787 Problem Description Jimmy exper...

iteye_4729的博客 224

HDU 1142 A Walk Through the Forest(最短路+dfs搜索)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8850Accepted Submission(s): 3267 Problem Description Jimmy ex...

weixin_30730151的博客 121

ACM HDU 1142 A Walk Through the Forest(单源最短路,记忆DFS)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2121    Accepted Submission(s): 756 Problem Description Jimmy exper...

weixin_34293059的博客 101

HDU 1142 A Walk Through the Forest(记忆化DFS+DIJKS最短路算法)

A Walk Through the ForestTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9641    Accepted Submission(s): 3545Problem Description Jimmy experienc...

满月照亮的路 186

HDU 1142 A Walk Through the Forest(dijkstra+记忆化搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 注意:这道题并不是求最短路的条数,而是求满足条件的条数(只要满足了条件,尽管到达家的路径不是最短的,但依然算是一条可行的路径),即找出所有满足一条路径的任意一条边A->B,A到终点的距离大于B到终点的距离的路径。 我们现在已经清楚了题目的要求...

asdfghjkl999999999的博客 3845

hdu 1142 A Walk Through the Forest(最短路径+dfs)

http://acm.hdu.edu.cn/showproblem.php?pid=1142 大致题意:若a,b之间有边,走ab边的条件是b到目的地的最短路径小于a到目的地的最短路径。按这个要求走,问最后从1到2的路径数目。 思路:先预处理到2的最短路径。然后从1dfs寻求路径数目。有点像DAG上的动态规划求路径数目。 #include #include #include

1104
上一篇: Doing Homework again(hdu1789,贪心)
下一篇: 昂贵的聘礼(poj1052,枚举+Dijstra)
lrbj
博客等级 码龄14年 11粉丝 228原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值