HDU 1142 最短路径的数量
来源:互联网 发布:涉密软件开发资质 编辑:程序博客网 时间:2024/06/05 19:24
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8767 Accepted Submission(s): 3237
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
题意:给n个点,m条边,问从1到2的最短路径的数量
做法 : dijkstra + 记忆化搜索
先求出2到各点的最短路径,然后从1开始记忆化搜索x到2的最短路径数量,如果i到x有边且i到2的最短距离小于x到i的最短距离,说明可能存在最短路径,递归下去
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#define maxn 1010#define inf 0x3f3f3f3f3f3fusing namespace std;typedef long long ll;int head[maxn];ll dis[maxn];ll path[maxn];int cnt=0;int n,m;int visited[maxn];struct Node{ int fr,to,next; ll val;}e[maxn*maxn];void add(int a,int b,ll c){ e[cnt].fr=a; e[cnt].to=b; e[cnt].val=c; e[cnt].next=head[a]; head[a]=cnt++;}void init(){ cnt=0; memset(head,-1,sizeof(head)); memset(dis,inf,sizeof(dis)); memset(path,0,sizeof(path)); memset(visited,0,sizeof(visited));}void dijkstra(int st){ int vis[maxn]; memset(vis,0,sizeof(vis)); dis[st]=0; ll mindis=inf; int k=1; for(int i=0;i<n;i++) { mindis=inf; for(int j=1;j<=n;j++) { if(!vis[j]&&dis[j]<mindis) { k=j; mindis=dis[j]; } } vis[k]=1; for(int j=head[k];j!=-1;j=e[j].next) { int to=e[j].to; if(!vis[to]&&dis[to]>dis[k]+e[j].val) { dis[to]=dis[k]+e[j].val; } } }}int dfs(int x){ if(path[x]) return path[x]; if(x==2) return 1; int ans = 0; int to; for(int i=head[x];i!=-1;i=e[i].next) { to=e[i].to; if(dis[x] > dis[to]) ans+= dfs(to); } return path[x] = ans;}int main(){ int a,b,c; while(scanf("%d",&n)==1&&n) { scanf("%d",&m); init(); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } dijkstra(2); dfs(1); printf("%d\n",path[1]); } return 0;}
- HDU 1142 最短路径的数量
- hdu 最短路径
- hdu 最短路径
- 一个人的旅行 HDU 最短路径
- poj 3463 Sightseeing 最短路径数量
- hdu 3790 最短路径问题(多权值的最短路)
- HDU 1874 最短路径
- hdu 2544最短路径
- HDU 1596 最短路径
- HDU 1874 最短路径
- HDU 2066 最短路径
- HDU 2544 最短路径
- HDU 2680 最短路径
- HDU 2112 最短路径
- HDU-3790-最短路径
- HDU 3790 最短路径
- hdu 2112 最短路径
- 【最短路径】hdu 3790
- 打造最强RecyclerView,Item侧滑菜单,长按拖拽Item,滑动删除Item
- MySQL中的统计信息相关参数介绍
- lua next 用法 table 空的判断
- solr6.0以上安装完,http://localhost:8080/solr/index.html,报404错
- Java String 类
- HDU 1142 最短路径的数量
- HashMap 实现原理
- 《剑指offer》输出最长回文子串
- Spring Boot集成redis做数据缓存
- svn和git 配置忽略文件
- Python自动上京东抢手机
- JS代码放在head和body中的区别分析
- 多用户配置
- Spring 事务管理问题