HDU 1142 A Walk Through the Forest
2012-08-21 14:23
375 查看
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3420 Accepted Submission(s): 1254
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
2
4
package hdu; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Queue; import java.util.Scanner; /** * @description HDU 1142 A Walk Through the Forest * @technique 最短路径,动态规划。 * @date 20120821 * @time 14:24 * @version 1.0 * @author Alex * */ public class Hdu1142_20120821_0 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n,m,temp,x,y,i; Queue<Integer> queue = new ArrayDeque<Integer>(); int [][] map; int [] dis; while(0!=(n = in.nextInt())){ m = in.nextInt(); map = new int ; dis = new int ; Arrays.fill(dis, Integer.MAX_VALUE); dis[1] = 0; for(i = 0; i < m; ++i){ x = in.nextInt() - 1; y = in.nextInt() - 1; temp = in.nextInt(); map[x][y] = temp; map[y][x] = temp; } queue.add(1); bellmanFord(map,queue,dis); buildTree(map,dis); queue.clear(); } } private static void bellmanFord(int [][] map, Queue<Integer> queue, int [] dis){ while(!queue.isEmpty()){ int i,j; i = queue.poll(); for(j = 0; j < map.length; ++j){ if(map[i][j] + dis[i] < dis[j] && map[i][j] != 0){ dis[j] = map[i][j] + dis[i]; if(!queue.contains(j)){ queue.add(j); } } } } } /** * 跟据最短路径,建立路径树。树叶子个数就是路径数。 * @param map * @param dis */ private static void buildTree(int [][] map, int[] dis){ int i,j; Node [] tree = new Node[dis.length]; for(i = 0; i < dis.length; ++i){ tree[i] = newNode(i); } Queue<Integer> queue = new ArrayDeque<Integer>(); queue.add(0); while(!queue.isEmpty()){ i = queue.poll(); for(j = 0; j < dis.length; ++j){ if(!tree[i].next.contains(j)&&map[i][j] != 0 && dis[i] > dis[j]){ tree[i].next.add(j); queue.add(j); } } } int [] dp = new int[dis.length]; Arrays.fill(dp, -1); dp[1] = 1; int count = trave(tree,0,dp); //遍历路径树。返回树叶子个数。 System.out.println(count); //输出结果。 } /** * 查找路径时使用动态规划的思想。不然会超时。 * @param tree * @param i * @param dp * @return 路径数。 */ private static int trave(Node [] tree, int i, int [] dp){ if(dp[i] != -1){ return dp[i]; } dp[i] = 0; for(int j = 0; j < tree[i].next.size(); ++j){ dp[i] += trave(tree,tree[i].next.get(j),dp); } return dp[i]; } private static Node newNode(int i){ return new Hdu1142_20120821_0().new Node(i); } private class Node{ int i; ArrayList<Integer> next; public Node(int i){ this.i = i; this.next = new ArrayList<Integer>(); } } }
相关文章推荐
- HDU 1142 A Walk Through the Forest (Dijkatra+记忆化搜索)(详解)
- HDU 1142 A Walk Through the Forest 最短路+记忆化搜索
- hdu 1142 A Walk Through the Forest
- 【Dijkstra+DFS(记忆化)】hdu 1142 A Walk Through the Forest(外:hdu 1428)
- hdu-1142-A Walk Through the Forest
- hdu 1142 A Walk Through the Forest
- Hdu-1142 A Walk Through the Forest
- HDOJ/HDU 1142 A Walk Through the Forest
- hdu-1142-A Walk Through the Forest-最短路+记忆化搜索
- HDU - 1142 - A Walk Through the Forest
- HDU 1142 A Walk Through the Forest【记忆化搜索+最短路Dijkstra算法】
- HDU 1142 A Walk Through the Forest (SPFA+记忆化搜索)
- 【解题报告】HDU -1142 A Walk Through the Forest
- hdu1142 A Walk Through the Forest
- hdu 1142 A Walk Through the Forest(最短路径+dfs)
- hdu 1142 A Walk Through the Forest 最短路+记忆化搜索 解题报告
- HDU 1142 欢迎参加——每周六晚的BestCoder(有米!) A Walk Through the Forest
- hdu1142-A Walk Through the Forest-最短路+dfs
- hdu 1142 A Walk Through the Forest(最短路+记忆化搜索)
- hdu 1142 A Walk Through the Forest(spfa求最短路+记忆化搜索)