A Walk Through the Forest(最短路径+深搜)
来源:互联网 发布:linux snmp mib库 编辑:程序博客网 时间:2024/06/10 00:59
描述:
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 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.
输出:
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.
样例输入:
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
样例输出:
2
4
题目大意:
此题题意很容易理解错容易误理解为求最短路的条数。真正的意思是:假设 A 和 B 是相连的,当前在 A 处,如果 A 到终点的距离大于 B 到终点的距离,则可以从 A 通往 B 处,问满足这种的条件的路径条数。一开始一直没看懂题意最后也是看别人博客懂得题目意思,心累
#include<stdio.h>#include<string.h>#define INF 0x3f3f3f3f//这里一开始尝试用INT_MAX代替INF不知道为何一直会出现问题 int map[1005][1005],asd[1005],check[1005],dis[1005],p[1005];int n,m;void dijkstra(int s)//套用dijkstra算法 { memset(check,0,sizeof(check)); int k,min; for(int i=1;i<=n;i++) { asd[i]=map[s][i]; } dis[s]=0; check[s]=1; for(int i=1;i<n;i++) { min=INF; for(int j=1;j<=n;j++) { if(!check[j]&&min>asd[j]) { min=asd[j]; k=j; } } dis[k]=min; check[k]=1; for(int j=1;j<=n;j++) { if(!check[j]&&asd[j]>map[k][j]+dis[k]) asd[j]=map[k][j]+dis[k]; } }}int dfs(int s){ if(p[s]) return p[s]; if(s==2) return 1; int i,sum=0; for(i=1;i<=n;i++) { if(map[s][i]<INF&&dis[s]>dis[i]) { if(p[i]) sum=sum+p[i]; else sum=sum+dfs(i); } } sum=sum+p[s]; p[s]=sum; return p[s];}int main(){ int a,b,c; while(scanf("%d",&n),n) { scanf("%d",&m); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) map[i][j]=0; else map[i][j]=INF; } } for(int i=0;i<m;i++) { scanf("%d %d %d",&a,&b,&c); map[a][b]=c;map[b][a]=c; } dijkstra(2);//利用dijkstra算法计算最短路径 memset(p,0,sizeof(p)); printf("%d\n",dfs(1));//利用深搜求得符合条件的路径数量 } return 0;}
附上一个dijkstra算法最短路径的链接:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/31/2615833.html
- A Walk Through the Forest(最短路径+深搜)
- A Walk Through the Forest(最短路径+DFS)
- hdu 1142 A Walk Through the Forest(最短路径+dfs)
- HDOJ 1142 A Walk Through the Forest(最短路径)
- HDU--1142--A Walk Through the Forest--深广搜/DP/最短路径/记忆化搜索
- HDU-1142 A Walk Through the Forest(最短路径+记忆化搜索)
- A Walk Through the Forest
- A Walk Through the Forest
- A Walk Through the Forest
- A Walk Through the Forest
- A Walk Through the Forest
- A Walk Through the Forest
- A Walk Through the Forest
- A Walk Through the Forest 最短路+dp
- hdu1142-A Walk Through the Forest-最短路+dfs
- A Walk Through the Forest(最短路+dfs)
- HDU 1142 A Walk Through the Forest 最短路+DFS
- A Walk Through the Forest(hdu1142,最短路+dfs)
- SDP协议分析
- 在其他应用程序vba中使用SQL
- 2017-8-14win7换win10 1703( creator)杂记
- 为kali安装32位运行时的库
- java利用cxf做客户端调用.net axis服务器
- A Walk Through the Forest(最短路径+深搜)
- 洛谷p3183食物链
- ecshop修饰符preg_replace/e不安全的几处改动
- DAO-DTO基础概念
- c++ override 保留字
- 分布式MySQL集群方案的探索与思考
- Qt 编译C++ boost库
- gitlab api获取所有用户的记录
- 【luogu1993】小 K 的农场(差分约束)