uva 10917

Problem C: A Walk Through the Forest


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
followingM 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

Output for Sample Input

2
4

根据终点走最短路,然后根据题意建图,易知为DAG,记忆化搜索求路径长度即可。

uva 10917,码迷,mamicode.com

时间: 2024-11-10 07:50:30

uva 10917的相关文章

UVA - 10917 Walk Through the Forest (最短路+DP)

题意:Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比所有从A出发回家的路径都短,你的任务是计算有多少条不同的路径 从后往前找最短路, 对于每一步要更新之后走的位置值: #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<algorithm> using namespace s

uva 10917 Walk Through the Forest(最短路)

uva 10917 Walk Through the Forest gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共有多少条不同的回家路径.其中起点的编号为1,终点的编号为2. Input 多组数据输入,每组数据第一行输入n,m(1<=n<=1000)表示点的数目和边的数目,点的编号为1~n,接下来m行每行输入3个数a,b,c表示有一条双向道路连接a,

UVA 10917 - Walk Through the Forest(dijstra)

UVA 10917 - Walk Through the Forest 题目链接 题意:公司编号为1,家编号为2,每次回家都不走回头路,回头路定义为:满足条件的道路(A,B),满足存在一条从B出发比所有从A出发的回家的路径都短,问有几种走法 思路:先从家求dijstra,这样满足条件的道路就是d[A] > d[B],这个图是一个dag,在上面进行dp就可以找出种数了 代码: #include <cstdio> #include <cstring> #include <v

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

layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: true mathjax: true tags: - 最短路 - 基础DP - Dijkstra - 图论 - 训练指南 Walk Through the Forest UVA - 10917 题意 Jimmy打算每天沿着一条不同的路走,而且,他只能沿着满足如下条件的道路(A,B):存在一条从B出发回家的路径,比

UVA 10917 Walk Through the Forest(Dijkstra+DAG动态规划)

题意:gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共有多少条不同的回家路径.其中起点的编号为1,终点的编号为2. 思路:首先从终点Dijkstra一次,求出每个点u回家的最短路长度,那么相当于创建了一个新图,当d[B]<d[A]时有一条A指向B的有向边,则题目的目标就是求出起点到终点的路径条数.因为这个图是DAG,可以用动态规划求解. #include

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 t

UVa 10917 林中漫步

https://vjudge.net/problem/UVA-10917 题意: 给出一个图,求出从1走到2共有多少种走法.前提是他只沿着满足如下条件的道路(A,B)走:存在一条从B出发回家的路径,比所有从A出发回家的路径都短. 思路: 首先用Dijkstra算法求出每个点到家的最短路径,那么题目的要求也就变成了d[B]<d[A],这样,我们创建了一个新的图,当且仅当d[B]<d[A]时加入有向边A->B,这样就是一个DAG,直接用动态规划计数. 1 #include <iostr

G - Walk Through the Forest (UVA - 10917)

- 题目大意 一个人,他只会沿着如下条件的道路(A,B)走:存在一条从B出发回家的路径,比所有从A出发回家的路径都要短.我们的任务是要找出一共有有多少条不同的回家路径. - 解题思路 先用dijkstra预处理出终点到每个点的最短路,然后将满足行走条件的A.B(除行走条件外,还要满足一个前提,即A.B之间要有边)用一条有向边连起来(A->B),然后利用记忆化搜索来解决这个问题. - 代码 #include<cstdio> #include<cstring> #include&

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d