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的更多相关文章

  1. UVA - 10917 - Walk Through the Forest(最短路+记忆化搜索)

    Problem    UVA - 10917 - Walk Through the Forest Time Limit: 3000 mSec Problem Description Jimmy exp ...

  2. 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)

    layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...

  3. A Walk Through the Forest[HDU1142]

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  4. hduoj----1142A Walk Through the Forest(记忆化搜索+最短路)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  5. 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 ...

  6. HDU 1142 A Walk Through the Forest (求最短路条数)

    A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...

  7. 【uva10917】Walk Through the Forest (最短路)

    题目: gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共 ...

  8. hdu_A Walk Through the Forest ——迪杰特斯拉+dfs

    A Walk Through the Forest Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/ ...

  9. 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 ...

随机推荐

  1. Android - 向服务器发送数据(POST) - HTTPClient.

    该篇文章主要说明使用Apache提供的HTTPClient,通过post方式,向服务器发送数据.由于有些东西在 Android - 向服务器发送数据(GET)中提到过,就不再重复. 一,Android ...

  2. Mysql DB2等数据库分页的实现

    一.Mysql的分页 (一).MySQL分页的实现,使用关键字:Limit    语法:select * from tableName Limit A,B; 注释:tableName:表名 A:查询的 ...

  3. iOS 跳转到应用所在的App Store市场

    代码入下 #import "ViewController.h" @interface ViewController ()<UIWebViewDelegate> @end ...

  4. java中 引用类型 和 基本类型 有何区别?

    栈与堆都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序员不能直接地设置栈或堆. Java的堆是一个运行时数据区,类的(对象从中分配空间.这些对象通过new.newa ...

  5. Linux Bash环境下单引(')、双引号(")、反引号(`)、反斜杠(\)的小结

    在bash中,$.*.?.[.].’.”.`.\.有特殊的含义.类似于编译器的预编译过程,bash在扫描命令行的过程中,会在文本层次上,优先解释所有的特殊字符,之后对转换完成的新命令行,进行内核的系统 ...

  6. 学习hamcrest和mockito时的总结和demo

    UT中需要的jar Junit4.1X.jar hamcrest-library-1.x.jar hamcrest-core-l.x.jar mockito-all-1.10.x.jar Junit ...

  7. TCP/IP笔记 应用层(3)——HTTP

    1. URL URL(Uniform Resource Locator) 相当于一个文件名在网络范围的扩展. 1.1 格式 schema://host[:port#]/path/.../[?query ...

  8. c++中的vector原理

    vectorvector就是动态数组.它也是在堆中分配内存,元素连续存放,有保留内存,如果减少大小后,内存也不会释放.如果新值>当前大小时才会再分配内存. 它拥有一段连续的内存空间,并且起始地址 ...

  9. SOSEx ReadMe

    Quick Ref:--------------------------------------------------bhi [filename] BuildHeapIndex - Builds a ...

  10. 从文章"避免复制与粘贴"到文章"Extract Method"的反思(2)

    好了.在上一篇里面讲了讲怎么把临时变量应该从函数里面剔除去.这个过程叫做从临时变量变成查询 那么接下来我们聊聊把代码提炼成函数,有叫做用函数对象取代函数 那么,问题来了:在函数中什么样的代码是需要被提 ...