题解报告:hdu 1142 A Walk Through the Forest
Problem Description
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
Output
Sample Input
Sample Output
#include<bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = ;
int n,m,a,b,c,dis[MAXN],pis[MAXN],G[MAXN][MAXN];
bool vis[MAXN];
void Dijkstra()
{
for(int i=;i<=n;i++)//把终点2当成起点,求出起点2到各节点的最短路径
dis[i]=G[][i];
dis[]=;vis[]=true;
for(int i=;i<n;i++){
int k=-;
for(int j=;j<=n;j++)
if(!vis[j] && (k==-||dis[j]<dis[k]))k=j;
if(k==-)break;
vis[k]=true;
for(int j=;j<=n;j++)
if(!vis[j])dis[j]=min(dis[j],dis[k]+G[k][j]);
}
}
int dfs(int x){//记忆化搜索
if(pis[x]!=-)return pis[x];
if(x==)return ;
pis[x]=;
for(int i=;i<=n;++i)//x到i有路且x到终点距离大于i到终点距离
if(G[x][i]!=INF && dis[x]>dis[i])pis[x]+=dfs(i);
return pis[x];
}
int main()
{
while(~scanf("%d",&n) && n){
scanf("%d",&m);
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
G[i][j]=(i==j?:INF);
memset(vis,false,sizeof(vis));
memset(pis,-,sizeof(pis));
for(int i=;i<=m;++i){
scanf("%d %d %d",&a,&b,&c);
G[a][b]=G[b][a]=c;
}
Dijkstra();
printf("%d\n",dfs());
}
return ;
}
题解报告:hdu 1142 A Walk Through the Forest的更多相关文章
- HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 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 ...
- 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 ...
- 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 ...
- 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 (Jav ...
- 【解题报告】HDU -1142 A Walk Through the Forest
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...
- hdu 1142 A Walk Through the Forest
http://acm.hdu.edu.cn/showproblem.php?pid=1142 这道题是spfa求最短路,然后dfs()求路径数. #include <cstdio> #in ...
- HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)
题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报 ...
- HDU 1142 A Walk Through the Forest(SPFA+记忆化搜索DFS)
题目链接 题意 :办公室编号为1,家编号为2,问从办公室到家有多少条路径,当然路径要短,从A走到B的条件是,A到家比B到家要远,所以可以从A走向B . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
随机推荐
- POJ 3468 线段树区间修改查询(Java,c++实现)
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...
- [bzoj1925][Sdoi2010][地精部落] (序列动态规划)
Description 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi, ...
- idea中找不到maven projects的集中解决办法
今天正常打开idea,却发现maven窗口找不到了:试了这些方法 首先idea自带了maven控件,不像Eclipse还需要下载控件,如果你以前有maven在右边,出于某种原因,消失找不到 了,你可以 ...
- Ajax学习总结(2)——Ajax参数详解及使用场景介绍
一.定义和用法 AJAX即"Asynchronous Javascript And XML"(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJA ...
- web & Rich Text Editor
web & Rich Text Editor 富文本编辑器 http://www.wangeditor.com/ https://github.com/wangfupeng1988/wangE ...
- [luoguP2038] 无线网络发射器选址(模拟)
传送门 又是个模拟水题,考虑边界就好,连long long都不用开. ——代码 #include <cstdio> #include <iostream> int n, d, ...
- 常州模拟赛d7t2 数组
题目背景 HJZ 有很多玩具.他最喜欢玩的玩具是一个可以变化的数组. 题目描述 HJZ 的数组初始时有 n 个元素.他可以把一个位置上的数加上或减去一个固定的 数 x. 一天 LJZ 和 HZY 来 ...
- 20170613NOIP模拟赛
共3道题目,时间3小时 题目非原创,仅限校内交流使用 题目名称 Graph Incr Permutation 文件名 graph incr permutation 输入文件 graph.in incr ...
- So easy
Problem Description Small W gets two files. There are n integers in each file. Small W wants to know ...
- mac os随笔
1.定义对象的成员变量的时候以便使用下划线开头:例如NSImage *_image; NSURL的一个函数: getResourceValue:forKey:error: Returns t ...