最短路
来源:互联网 发布:九死惊陵甲 知乎 编辑:程序博客网 时间:2024/04/30 18:11
A Walk Through the Forest
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 4
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.
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 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10
Sample Output
24
题意:寻找一共有多少条符合题意的路。即:能从A点走到点B(点A到终点的最短路>点B到终点的最短路)因此,从终点出发,求每一个点的最短路(dijkstra()),然后那些最短路的值记录起来,作为能否通过的判断条件。最后用记忆化搜索来搜索出一共多少条符合条件的路(dfs());#include<iostream>#include<cstdio>#include<string.h>#define maxint 0xfffffff#define max 1001using namespace std;int n,m;//十字路口数int map[max][max];int dis[max];int vis[max];int d[max];void init(){ int i,j; for(i=1;i<max;i++) for(j=1;j<max;j++) { map[i][j]=maxint; }}void dijkstra(int tem){ int i,j; for(i=1;i<=n;i++) { vis[i]=0; dis[i]=map[i][tem]; } vis[tem]=1; dis[tem]=0; for(i=1;i<n;i++) { int mid=0; int d=maxint; for(j=1;j<=n;j++) { if(vis[j]==0&&d>dis[j]) { d=dis[j]; mid=j; } } vis[mid]=1; if(d==maxint) break; for(j=1;j<=n;j++) { if(vis[j]==0&&map[j][mid]<maxint&&map[j][mid]+d<dis[j]) dis[j]=map[j][mid]+d; } }}int dfs(int v){ int i; if(v==2) return 1; if(d[v]) return d[v]; int ans=0,temp; for(i=1;i<=n;i++) { if(map[v][i]!=maxint&&dis[v]>dis[i])//有路相通,要去的i点到终点的距离比v点到终点的距离小 { temp=dfs(i); ans+=temp; } } d[v]=ans; return ans;}int main(){ int a,b,di; while(scanf("%d",&n),n) { init(); scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&di); map[a][b]=map[b][a]=di; } memset(d,0,sizeof(d)); dijkstra(2); printf("%d\n",dfs(1)); } return 0;}
- 最短路 & 次短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- Delphi 函数指针 过程/函数类型
- 【转载】数据库的几种连接查询
- 第十四节 html5学习——Input
- 最短路
- 一顿牛排——自动发帖回复机
- 最短路
- C++ Primer 第二遍阅读笔记(第一章)
- ASP.NET MVC2 Model验证
- eclipse+pydev快捷键
- 召回率 Recall、精确度Precision、准确率Accuracy
- hdu2046 骨牌铺方格
- 第十五节 html5学习——表单元素
- 软件编程 我是一个菜鸟
- HEVC学习(二十八) —— 量化之二