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 ...
随机推荐
- python文件操作汇总
1.创建文件 f = open(filename,'w+')
- iOS消息推送机制
iOS消息推送的工作机制可以简单的用下图来概括: Provider是指某个iPhone软件的Push服务器,APNS是Apple Push Notification Service的缩写,是苹果的服务 ...
- linux下安装svn(基于编码的方式)
svn是什么,相信能看到这里的同学应该不会有这个问题了,费话不多说,开始: 1.创建目录 mkdir /home/svn/ 2.获取安装svn所需源文件(svn的官方网址是http://subvers ...
- python学习笔记--随时更新
# coding=GBK score = 90 if score >= 80: print("好") elif score >= 60: print("及格& ...
- cmake简易教程
用cmake替代makefile,构建项目还是蛮简单实用的. 工程目录下src放源代码,build保存所有的编译过程和结果. 首先看看src目录下的源代码结构: 最顶层CMakeLists.txt内容 ...
- sublime 正则搜索日语字符
sublime 正则搜索日语字符 [\x{3041}-\x{3096}\x{30A0}-\x{30FF}\x{3400}-\x{4DB5}\x{4E00}-\x{9FCB}\x{F900}-\x{FA ...
- R 语言DataFrame 排序
Sort:dd <- data.frame(b = factor(c("Hi","Med","Hi","Low") ...
- PreResultListener使用
PreResultListener是一个监听器接口,可以在Action处理完之后,系统转入实际视图前被回调. Struts2应用可以给Action.拦截器添加PreResultListener监听器, ...
- QQ登录接口(第三方登录接口)
CI框架 QQ接口(第三方登录接口PHP版) 本帖内容较多,大部分都是源码,要修改的地方只有一个,其他只要复制过去,就可以完美运行.本帖主要针对CI框架,不用下载SDK,按我下面的步骤,建文件,复制代 ...
- C#小数点位数处理方法
//方法一: //保留小数位数,并能四舍五入 DecimalFormat de = new DecimalFormat("0.00"); System.out.println(de ...