HDU--1142--A Walk Through the Forest-Deep Search/DP/Shortest Path/Memory search

Source: Internet
Author: User

HDU--1142--A Walk Through the Forest-Deep Search/DP/Shortest Path/Memory search

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission (s): 5948 Accepted Submission (s): 2191

Problem DescriptionJimmy 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. calculate how many different routes through the forest Jimmy might take.

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

OutputFor 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
24

Question: The same as HDU1978, but there is a map. Here is the sparse matrix. Enter information a, B, for group M, c indicates that the distance from a to B is c, and calculates the number of routes from 1 to 2. requirement: the premise that A can go to B is that the shortest path from B to 2 is shorter than that of.

Resolution: There is nothing to say. Make the shortest distance from 2 to reach each point, and then start from 1 to follow the descending distance to search



# Include
  
   
# Include
   
    
# Include
    
     
# Define Max 2147483648 using namespace std; int n, dd [4] [2] = {, 0,-, 0,-} ;__ int64 m, dp [1111], dis [1111], mm [1111] [1111]; // use 64-bit, otherwise the int vis [2147483648]; void dj () // djkstra algorithm, dijela {int I, j, k, l; _ int64 Min; memset (vis, 0, sizeof (vis); for (I = 1; I <= n; I ++) dis [I] = mm [2] [I]; dis [2] = 0; for (I = 1; I <= n; I ++) {Min = Max; for (j = 1; j <= n; j ++) // find the if (vis [j] = 0 & Min> dis [j]) closest to 2 from the unaccessed point {Min = dis [j]; // record minimum value l = j; // record subscript} if (Min = Max) break; // if not found, end vis [l] = 1; // mark that this point has been accessed for (j = 1; j <= n; j ++) // use this closest point to update other points, this step is called relaxation {if (dis [j]> dis [l] + mm [l] [j]) // relaxation {dis [j] = dis [l] + mm [l] [j] ;}}__ int64 dfs (int x) // search {int I, j, k, l; if (dp [x]) return dp [x] based on the minimum path value obtained previously; // perform the memory-based search operation to avoid repeated searches for (I = 1; I <= n; I ++) if (mm [x] [I]
     
      
C) // you are not sure whether there is a duplicate because you haven't read the question. If you write this code, it won't be wrong. mm [a] [B] = mm [B] [a] = c ;} dj (); memset (vis, 0, sizeof (vis); memset (dp, 0, sizeof (dp); dp [2] = 1; dfs (1 ); printf ("% I64d \ n", dp [1]);} return 0 ;}
     
    
   
  

Conclusion: The more I write, the more I feel comfortable with writing and memorizing search. I feel like I can't stop working with my sister.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.