最短路+DP

Walk Through the Forest
Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]

Description

Problem C: A Walk Through the Forest

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

Output for Sample Input

2
4

(apologies to) Richard Krueger

[Submit]   [Go Back]   [Status]

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int maxn=1010,INF=0x3f3f3f3f;int g[maxn][maxn],n,m,dist[maxn];
bool vis[maxn];void dijkstra(int st)
{memset(vis,0,sizeof(vis));memset(dist,63,sizeof(dist));dist[st]=0;for(int i=1;i<=n;i++){int mark=-1,mindist=INF;for(int j=1;j<=n;j++){if(!vis[j]&&dist[j]<mindist){mindist=dist[j];mark=j;}}if(mark==-1) break;vis[mark]=true;for(int j=1;j<=n;j++){if(!vis[j]&&dist[j]>dist[mark]+g[mark][j])dist[j]=dist[mark]+g[mark][j];}}
}int dp[maxn];int dfs(int u)
{if(dp[u]!=-1)return dp[u];int res=0;for(int i=1;i<=n;i++){if(g[u][i]!=INF&&dist[i]<dist[u])res+=dfs(i);}return dp[u]=res;
}int main()
{while(scanf("%d",&n)!=EOF&&n){scanf("%d",&m);memset(g,63,sizeof(g));for(int i=0;i<m;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);g[a][b]=g[b][a]=c;}dijkstra(2);memset(dp,-1,sizeof(dp));dp[2]=1;printf("%d\n",dfs(1));}return 0;
}

UVA 10917 Walk Through the Forest相关推荐

  1. A Walk Through the Forest HDU - 1142(dijkstra+动态规划)

    题目大意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路, 且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的 一条路.问满足这样的路径条数 有多少... 解题思路 ...

  2. A Walk Through the Forest

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 题解: 看样子很多人都把这题目看错了,以为是求最短路的条数.真正的意思是:假设 A 和 B 是相连的,当前 ...

  3. HDU 1142 A Walk Through the Forest dijkstra + DFS

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 题意: Jimmy在位置 1 ,每天晚上要回位置2(家),计算1到2的最短距离,Jimmy要先去一个地方然 ...

  4. A Walk Through the Forest dijkstra(邻接矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=1142 dijkstra(邻接矩阵) 1 #include <cstdio> 2 #include & ...

  5. UVA10917 Walk Through the Forest

    题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 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 ...

  6. π-Algorithmist分类题目(3)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(3) Probability ...

  7. π-Algorithmist分类题目(2)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(2) Set Theory U ...

  8. 最短路算法详解(Dijkstra/SPFA/Floyd)

    转自:http://blog.csdn.net/murmured/article/details/19281031 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短 ...

  9. sicily题目分类

    sicily题目分类 1. 编程入门 2. 数据结构 3. 字符串 4. 排序 5. 图遍历 6. 图算法 7. 搜索:剪枝,启发式搜索 8. 动态规划/递推 9. 分治/递归 10. 贪心 11. ...

最新文章

  1. python爬虫吧-Python爬虫案例集合
  2. pyqt5 中QSS
  3. ITK:从图像中提取轮廓
  4. Documentum常见问题4—如何通过vlink方式直接查看文档内容
  5. VUE如何操作DOM
  6. java中负数取整_Java取整,固定保留两位小数,适配负数、金融数字。
  7. 影场与属性访问器界面
  8. python web应用_为您的应用选择最佳的Python Web爬网库
  9. 洛达检测软件AB1562UT_1.4.4新版本下载,适用洛达全系列
  10. 什么是参数化设计,通过实操了解一下? | SOLIDWORKS 操作视频
  11. Bailian4108 羚羊数量-Number Of Antelope【递推+打表+递归+记忆化递归】
  12. 深入浅出业务幂等性---4、消息幂等
  13. 计算机换系统后word丢失怎么办,Word文件丢失如何找回?教你解决文件丢失的难题...
  14. SpringCloud知识概括
  15. 实现DOC、DOCX转换为PDF 再将PDF转换为图片
  16. linux学习笔记2——ls命令说明
  17. 2021年数学建模国赛C题问题二详细思路和代码
  18. 日化用品行业智能供应链协同系统解决方案:数智化SCM供应链,为企业转型“加速度”
  19. Oracle函数篇 - lpad 函数
  20. 网络基础之OSI七层参考模型与TCP/IP五层模型

热门文章

  1. java类中定义索引器,C#面向对象基础——字段、属性和索引器
  2. 东财计算机应用基础在线作业答案,《计算机应用基础》东财在线20秋第一套作业答案...
  3. android调用c闪退未看到log,解决:Android开发 看不到崩溃的日志
  4. MySQL二进制日志的三种模式解析
  5. [NOI2015]品酒大会
  6. Spring Boot有四大神器
  7. Win10+Python+Django+Nginx+MySQL开发教程及实例(1)——开发环境搭建
  8. jquery 前台分页插件总结(1 前台假分页 2 后台分页)
  9. 《代码大全》阅读笔记02
  10. hibernate小记