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. [Mime] QuotedPrintableEncoding帮助类 (转载)

    点击下载 QuotedPrintableEncoding.rar 这个类是关于QuotedPrintableEncoding的帮助类看下面代码吧 /// <summary> /// 类说明 ...

  2. Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException

    异常Log: Error:Execution failed for task ‘:app:dexDebug’. > com.android.ide.common.process.ProcessE ...

  3. Javascript基础学习(3)_对象和数组

    一.对象是一种无序的属性集合,每个属性都有自己的名字和值. 1.创建对象 花括号内逗号分隔 var person = { "Name" : "LiCheng", ...

  4. C# 绘制窗体客户非客户区要用WM_PAINT和WM_NCPAINT

    窗体分为两部分:客户区(Client area)和非客户区(Non-Client area) WM_PAINT消息.OnPaint()方法.GetDC()API函数都是处理窗体客户区绘制的   而标题 ...

  5. Dice (III) 概率dp

    #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> ...

  6. 插入标记 方法 insertAdjacentHTML

    html5新增的插入标记方法,insertAdjacentHTML() 可以接受2个参数 插入位置和要插入的 HTML 文本.第一个参数必须是下列值之一: "beforebegin" ...

  7. easy ui tree 取父节点的值和取蓝色框的值

    var nodes = $('#basetree').tree('getChecked'); var cnode = ''; var fnode = ''; for ( var i = 0; i &l ...

  8. PHP设计模式之:策略模式

    <?phpabstract class Strategy{    public abstract function AlgorithmInterface();} class ConcreteSt ...

  9. pushState与replaceState区别

    history.pushState(state, title, url) 将当前URL和history.state加入到history中,并用新的state和URL替换当前.不会造成页面刷新. sta ...

  10. 桂电在线-转变成bootstrap版3(记录学习bootstrap)

    继续上文 正文菜单 html: <!-- 菜单块 --> <div class="on-light" id="menus"> <s ...