UVa 10917 A Walk Through the Forest
A Walk Through the Forest
Time Limit:1000MS Memory Limit:65536K
Total Submit:48 Accepted:15
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
【思路】
最短路+记忆化搜索。
SPFA预处理出每个点到达home的最短距离d,然后沿着d变小的边记忆化搜索路径条数。
【代码】
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
using namespace std; const int maxn = +;
const int INF=<<;
struct Edge{
int u,v,w,next;
}e[*maxn*maxn];
int en,front[maxn]; int n,m; inline void AddEdge(int u,int v,int w) {
en++; e[en].v=v; e[en].w=w; e[en].next=front[u]; front[u]=en;
} int d[maxn];
void SPFA(int s) {
int inq[maxn];
queue<int> q;
memset(inq,,sizeof(inq));
for(int i=;i<=n;i++) d[i]=INF; d[s]=; inq[s]=; q.push(s);
while(!q.empty()) {
int u=q.front(); q.pop(); inq[u]=;
for(int i=front[u];i>=;i=e[i].next) {
int v=e[i].v,w=e[i].w;
if(d[v]>d[u]+w) {
d[v]=d[u]+w;
if(!inq[v]) {
inq[v]=;
q.push(v);
}
}
}
}
} int f[maxn];
int dp(int u) {
int& ans=f[u];
if(ans>=) return ans; if(u==) return ans=;
ans=;
for(int i=front[u];i>=;i=e[i].next) {
int v=e[i].v;
if(d[v]<d[u]) ans += dp(v); //只沿着d更小的走
}
return ans;
}
int main() {
while(scanf("%d%d",&n,&m)==) {
en=-;
memset(front,-,sizeof(front));
int u,v,w;
for(int i=;i<m;i++) {
scanf("%d%d%d",&u,&v,&w);
AddEdge(u,v,w);
AddEdge(v,u,w);
}
SPFA();
memset(f,-,sizeof(f));
printf("%d\n",dp());
}
return ;
}
UVa 10917 A Walk Through the Forest的更多相关文章
- UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)
Problem UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...
- 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)
layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...
- 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 ...
- 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 (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...
- 【uva10917】Walk Through the Forest (最短路)
题目: gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共 ...
- hdu_A Walk Through the Forest ——迪杰特斯拉+dfs
A Walk Through the Forest Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/ ...
- 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/O ...
随机推荐
- sql脚本的格式
创建表前先判断是否存在 IF OBJECT_ID(N'TableDataDictionary') IS NULL 存储过程头:--=================================== ...
- html-----006
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Javascript中bind()方法的使用与实现
对于bind,我愣了下,这个方法常用在jquery中,用于为被选元素添加一个或多个事件处理程序. 查了下手册,发现bind的作用和apply,call类似都是改变函数的execute context, ...
- OJ的文件流操作
我们刷题的时候除了编码外,测试也是非常重要的,当测试样例比较小的时候,我们完全可以手打,但是当测试样例比较大时候,我们就抓狂了~ 相信不少人都知道利用文件流,但是应该还有新手跟我一样,一遍又一遍地输入 ...
- PHP设计模式之:建造者模式
建造者模式: 将一个复杂对象的构造与它的表示分离,使同样的构建过程可以创建不同的表示的设计模式; 目的: 消除其他对象复杂的创建过程 结构图: 优点: 建造者模式可以很好的将一个对象的实现与相关的“业 ...
- angularjs应用骨架
使用典型的类库时,你可以选择并使用你所喜欢的功能:而对于angularjs框架来说,必须把它看成一个完整的套件来使用,框架中的所有的东西都包含在里面,接下来将会介绍angular的基础模块,这样你就可 ...
- 经典SQL练习题
题目地址:http://blog.csdn.net/qaz13177_58_/article/details/5575711 1. 查询Student表中的所有记录的Sname.Ssex和Class列 ...
- apache日志文件 accesslog
因为想要看到apache的日志记录用户请求某个页面所花的时间,需要添加额外参数才会记录,所以临时查了下哦..记下来了 在httpd.conf里可以看到一行这样的配置 LogFormat "% ...
- 局部视图(partial)
局部视图(partial) 原文:Partial Views作者:Steve Smith翻译:张海龙(jiechen).刘怡(AlexLEWIS)校对:许登洋(Seay).何镇汐.魏美娟(初见) AS ...
- reverse string | leetcode
思路:在原来的字符串后面添加上strlen-1个字符,返回 class Solution { public: string reverseString(string s) { unsigned int ...