HDU1142 A Walk Through the Forest(最短路+DAG)
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 ;
}
HDU1142 A Walk Through the Forest(最短路+DAG)的更多相关文章
- HDU1142 A Walk Through the Forest(dijkstra)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- A Walk Through the Forest (最短路+记忆化搜索)
Jimmy experiences a lot of stress at work these days, especially since his accident made working dif ...
- hdu1142 A Walk Through the Forest( Dijkstra算法+搜索)
看到这道题,想起了我家旁边的山! 那是一座叫做洪山寨的山,据说由当年洪秀全的小妾居住于此而得名! 山上盛产野果(很美味)! 好久没有爬上去了! #include<stdio.h> #inc ...
- UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)
用新模板阿姨了一天,换成原来的一遍就ac了= = 题意很重要..最关键的一句话是说:若走A->B这条边,必然是d[B]<d[A],d[]数组保存的是各点到终点的最短路. 所以先做dij,由 ...
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
- HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 1142 A Walk Through the Forest(最短路+dfs搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- A Walk Through the Forest[HDU1142]
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
随机推荐
- Spark 贝叶斯分类算法
一.贝叶斯定理数学基础 我们都知道条件概率的数学公式形式为 即B发生的条件下A发生的概率等于A和B同时发生的概率除以B发生的概率. 根据此公式变换,得到贝叶斯公式: 即贝叶斯定律是关于随机事件A和B ...
- Project 8:利用递归算法求最大值
目标:用递归算法实现求一个数组中的最大元素. 样例输入 5 1 4 2 5 3 样例输出 5 #include <stdio.h> int max(int *,int); int main ...
- Ubuntu操作系统下安装JDK、tomcat、mysql
1.先从安装虚拟机开始 01.首先打开VMware虚拟机. 02.然后,进入home主页,点击"create a New Virtual Machine"一栏,就会弹出一个 ...
- NHibernate学习教程(6)--事务Transactions
本节内容 事务概述 1.新建对象 [测试成功提交] [测试失败回滚] 2.删除对象 3.更新对象 4.保存更新对象 结语 上一篇我们介绍了NHibernate中的Insert, Update, De ...
- hibernate 教程(3)—NHibernate查询语言HQL
NHibernate之旅(3):探索查询之NHibernate查询语言(HQL) 本节内容 NHibernate中的查询方法 NHibernate查询语言(HQL) 1.from子句 2.select ...
- 团队作业4——第一次项目冲刺 tHiRd DaY
项目冲刺--Triple Kill 小编又来了,好困呐,上了一天的课还要写博客,为什么写博客的一直是我呢..一点乐子都没有*-* 但是我还是得写啊[我也很无奈啊],那就让我给大家找点乐子吧 天霸动霸. ...
- 团队作业8——Beta版本冲刺计划及安排
团队作业8--Beta版本冲刺计划及安排 经过紧张的Alpha阶段,很多组已经从完全不熟悉语言和环境,到现在能够实现初步的功能.下一阶段即将加快编码进度,完成系统功能.强化软件工程的体会. 凡事预则立 ...
- return 的使用
(一):while中使用return //编译不通过,编译器不知道isTrue()方法是否会返回true,这样不能test()方法一定有返回值. public String test(){ while ...
- 团队作业4——第一次项目冲刺(Alpha版本)第五天
天气阴转晴 一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.界面 功能界面已经大致完成 实现判断学生答题正误的界面 2.出题方面 实现错题库的构造 四.困难与问题 1.项 ...
- 201521123057 《Java程序设计》第2周学习总结
1. 本章学习总结 本周Java教学主要围绕Java的基本语法展开.在本周的学习中,我了解到了: -Integer的取值范围 -if...else条件式,switch条件式,while循环,break ...