题解报告: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 . 思路 : 先以终点为起点求最短路,然后记忆化搜索 ...
随机推荐
- Python习题之列表排序,4种方法
def sort_list_method_1(a): return sorted(a) print(sort_list_method_1([1, 4, 2])) def sort_list_metho ...
- Leetcode题目practice
目录 Leetcode题目解答 1. 删除最外层的括号 2. 两数之和 3. 宝石与石头 4. 移除元素 5.删除排序数组中的重复项 6.寻找两个有序数组的中位数 7.盛最多水的容器 8.存在重复元素 ...
- 洛谷 2777 [AHOI2016初中组]自行车比赛
[题解] 为了让某个选手能够获得总分第一,就让他最后一天的得分是n,并且让别的选手的得分的最大值尽量小.于是我们先把目前积分排序,并且让他们最后一天的排名刚好与积分排名相反.即某个积分排名为X的人最后 ...
- BZOJ 4415 洛谷 3988 [Shoi2013]发牌
[题解] 权值线段树.查询当前牌堆顶的牌并且删掉就好了. #include<cstdio> #include<algorithm> #define N 3000010 #def ...
- Spring MVC 概述
[简介] Spring MVC也叫Spring web mvc,属于表现层的框架.SpringMVC是Spring框架的一部分,是在Spring 3.0后发布的. 由以上Spring的结构图可以看出, ...
- 对jetbrains全系列可用例:IDEA、WebStorm、phpstorm、clion等----https://blog.csdn.net/u014044812/article/details/78727496
https://blog.csdn.net/u014044812/article/details/78727496 pyCharm最新2018激活码
- HDU 4903 (模拟+贪心)
Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a he ...
- 从尾到头打印链表——剑指Offer
https://www.nowcoder.net/practice/d0267f7f55b3412ba93bd35cfa8e8035?tpId=13&tqId=11156&tPage= ...
- C++ - 模板函数须要类型转换时使用友元(friend)模板函数
模板函数须要类型转换时使用友元(friend)模板函数 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24357301 非模板函数 ...
- 1.7-BGP④
注意:默认路由ip route 0.0.0.0 0.0.0.0 12.1.1.1是不可以作为BGP邻居TCP始发连接的(但回包可以) 要配静态路由:ip route 13.1.1.3 255.255. ...