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 ...
随机推荐
- Android手势解锁, 九宫格解锁
给大家介绍一个很好用的手势解锁控件ShapleLocker, 废话不多先上效果图: 这是一个第三方库, 可自己根据UI需求替换图标: 圆圈, 小箭头等等.. github地址: http://pane ...
- Android MVP模式的初识
MVP是什么?或许在之前更多的人知道的是MVC这个模式(Model View Controller),然而MVP与MVC最不同的一点是M与V是不直接 关联的也是就Model与View不存在直接关系 ...
- c语言学习之基础知识点介绍(十):内存空间模型、地址解释及指针变量
一.内存 /* 内存: 存在内存里的. 内存分了N多个小空间,每个小空间1个字节 每个小空间有它自己的地址.每个地址之间差1 int类型占用4个字节,等于占了4个空间(有4个地址),不需要记住4个地址 ...
- C#自定义线程池
自定义线程池-c#的简单实现 下面是代码,希望大家提出更好的建议: 1.ThreadManager.cs using System; using System.Threading; using Sys ...
- cas系列(三)--HTTP和HTTPS、SSL
(这段时间打算做单点登录,因此研究了一些cas资料并作为一个系列记录下来,一来可能会帮助一些人,二来对我自己所学知识也是一个巩固.) 本文转自異次元藍客点击打开链接 1. HTTPS HTTPS(全 ...
- jsoup:解析HTML用法小结
1.解析方式 (1)从字符串解析 ? 1 2 3 String html = "<html><head><title>First parse</ti ...
- OC - 14.NSOperation与NSOperationQueue
简介 通过NSOperation与NSOperationQueue的组合也能实现多线程 通常将任务封装成NSOperation对象,并将对象添加到NSOperationQueue中实现 NSOpera ...
- 冒泡排序(C++)
冒泡排序(C++) 冒泡排序(C++): 原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换, ...
- http请求的组成部分
报文流 1.HTTP 报文是在HTTP 应用程序之间发送的数据块.这些数据块以一些文本形式的元信息(meta-information)开头,这些信息描述了报文的内容及含义,后面跟着可选的数据部分.这些 ...
- ES 的CRUD 简单操作(小试牛刀)
URL的格式: http://localhost:9200/<index>/<type>/[<id>] 其中index.type是必须提供的. id是可选的,不提供 ...