A Walk Through the Forest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9081 Accepted Submission(s): 3353 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

学到了DAG中如何记忆化(也可以按距离排序后处理)

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<memory.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=;
const int maxm=;
int Laxt[maxn];
int len[maxm],To[maxm],Next[maxm],cnt,ans;
int dis[maxn],used[maxn],p[maxn];
queue<int>q;
void _add(int u,int v,int d)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
len[cnt]=d;
}
void _dij(){
while(!q.empty()){
int u=q.front();q.pop();
used[u]=;
for(int i=Laxt[u];i;i=Next[i]){
if(dis[To[i]]>dis[u]+len[i]){
dis[To[i]]=dis[u]+len[i];
if(!used[To[i]])
q.push(To[i]);
}
}
}
}
int _find(int v)
{
if(p[v]) return p[v];
int sum=;//使用sum而不是直接p[v]加,保证了加完后才使用,保证了DAG的方向性。
for(int i=Laxt[v];i;i=Next[i])
if(dis[To[i]]<dis[v])
sum+=_find(To[i]);
p[v]=sum;
return p[v];
}
int main()
{
int n,m,i,j,k,x,y,z;
while(~scanf("%d",&n)){
if(n==) return ;
scanf("%d",&m);
cnt=ans=;
memset(dis,0x7F,sizeof(dis));
memset(Laxt,,sizeof(Laxt));
memset(used,,sizeof(used));
memset(p,,sizeof(p));
for(i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
_add(x,y,z);
_add(y,x,z);
}
p[]=;
dis[]=;
used[]=;
q.push();
_dij();
printf("%d\n",_find());
}
return ;
}

最新文章

  1. Zotero 使用指南
  2. 部署war包后,新增tomcat服务器,启动tomcat服务器报错解决方法
  3. activiti 介绍
  4. 编译在arm板上使用的sqlite3的静动态库
  5. CLR探索应用程序域世界(上):Windbg SOS剖析揭示域世界
  6. 【综述】(MIT博士)林达华老师-&quot;概率模型与计算机视觉”
  7. ios ReactiveViewModel
  8. g++编译cpp文件
  9. SDUTOJ 1489 求二叉树的先序遍历
  10. 使用Dapper进行参数化查询
  11. ubantu服务器配置ss
  12. 游戏脚本编程 文本token解析
  13. NetCore开源项目集合
  14. 结构体内嵌比较函数bool operator &lt; (const node &amp;x) const {}
  15. html DOM簡介
  16. 项目冲刺Forth
  17. Utf-8+Bom编码导致的读取数据部分异常问题
  18. git 权限问题:insufficient permission for adding an object to repository database .git
  19. 零基础学习hadoop到上手工作线路指导(初级篇)
  20. 基于VUE的SPA单页应用开发-加载性能篇

热门文章

  1. spring报错NoClassDefFoundError等与第三方jar包导入问题
  2. Calico 的网络结构是什么?- 每天5分钟玩转 Docker 容器技术(68)
  3. 排序--SelectionSort 选择排序
  4. js实现换肤效果
  5. NHibernate教程(12)--延迟加载
  6. 标题:a++和++a的区别
  7. python--简易购物车实现
  8. CentOS7中将home迁移到/下的命令 CentOS7中将home迁移到/下的命令
  9. 【1414软工助教】团队作业6——展示博客(Alpha版本) 得分榜
  10. Java计算器1.0版