HDU 1142 A Walk Through the Forest
来源:互联网 发布:新华教务网络管理系统 编辑:程序博客网 时间:2024/05/19 10:40
A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3436 Accepted Submission(s): 1261
Problem 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.
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 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10
Sample Output
24package 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最短路径,动态规划。 * @date20120821 * @time14:24 * @version1.1 * @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;int [] dp;while(0!=(n = in.nextInt())){m = in.nextInt();map = new int[n][n];dis = new int[n];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);dp = new int[dis.length];Arrays.fill(dp, -1);dp[1] = 1;int count = trave(map,0,dp,dis);System.out.println(count);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 */@SuppressWarnings("unused")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 int trave(int [][] map,int i, int [] dp, int [] dis){if(dp[i] != -1){return dp[i];}if(dp[i] == 1){return dp[i];}dp[i] = 0;for(int j = 0; j < map.length; ++j){if(dis[i] > dis[j] && map[i][j] != 0)dp[i] += trave(map,j,dp,dis);}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>();}}}
PS: 这个代码是上一次那个代码的改写,上次代码建了一个路径树。这次用的是记忆化搜索。两次代都在这里,可以对比看,现以这个版本比上个版本在时间上快了 1000ms
- HDU 1142 A Walk Through the Forest
- A Walk Through the Forest HDU 1142
- 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
- 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
- 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
- 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
- [Sql Server] 行转列 <一>------静态转换
- DBI接口与DPI接口与DSI接口
- 在别的函数或者线程函数里使用 PostMessage(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)小收获
- 绝对路径 ${pageContext.request.contextPath}
- 使用MySQL命令行修改密码
- HDU 1142 A Walk Through the Forest
- IBM --AIX 常用命令
- 提高你开发效率的十五个 Visual Studio 使用技巧
- Oracle中GOTO的用法
- hdu——1999(不可模数 )
- Google开源实时通信项目WebRTC
- DLL延迟加载工程分析
- [Ubuntu]: 10.04当中安装使用sunpinyin
- 13个 ASP.NET MVC 的扩展