Hdu--1142--a Walk Through The forest--deep search/dp/Shortest path/memory search

Source: Internet
Author: User

A Walk Through the ForestTime 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 he accident made working diffic Ult. To relax after a hard day, he likes to walk home. To make things even nicer, he office is on one side of a forest, and he house was on the other. A Nice walk through the forest, seeing the birds and chipmunks are 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 is always takes a path to make progress towards his house. He considers taking a path from a to B to being progress if there exists a route from B to his home that's shorter than any Possible route from A. Calculate how many different routes through the forest Jimmy might take.

Inputinput contains several test cases followed by a line containing 0. Jimmy had numbered each intersection or joining of paths starting with 1. His office is numbered 1, and he house is numbered 2. The first line of all 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 o F Length D between intersection A and a different intersection B. Jimmy may walk a path any direction he chooses. There is at the most one of the path between any pair of intersections.

Outputfor each test case, output a single integer indicating the number of different routes through the forest. Assume that is 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

Test instructions: and HDU1978 the same topic, but there is the map, here is the sparse matrix to give you m-group input information a,b,c represents a to B distance of C, the number of routes from 1 to 2, requirements: A can go to b the premise is B to 2 of the shortest short than a short

Resolution: There is nothing to say, from 2 to make the shortest distance to each point, and then from 1 to follow this distance descending to search



#include <iostream> #include <cstdio> #include <cstring> #define MAX 2147483648using namespaceStd;intN,Dd[4][2]={0,1,0,-1,1,0,-1,0};__int64M,Dp[1111],Dis[1111],Mm[1111][1111];//to use 64 bits, or 2147483648 will go wrong.intVis[1111];voidDj()//djkstra algorithm, Dijkstra {intI,J,K,L;__int64Min;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++) /  /from points that have not been visited to find the distance 2 nearestif(Vis[J]==0&&Min>Dis[J])        {Min=Dis[J]; /  /record minimum valueL=J; /  /record subscript}if(Min==Max) Break; /  /If not found, endVis[L]=1; /  /Mark this point has been visited for(J=1;J<=N;J++) /  /Use this nearest point to update other points, the term of this step is called relaxation {if(Dis[J]>Dis[L]+Mm[L][J])//slack {Dis[J]=Dis[L]+Mm[L][J]; }        }    }}__int64Dfs(intX)//Follow the shortest path value as previously calculated to search {intI,J,K,L;if(Dp[X])returnDp[X]; /  /Memory search operation, avoid repeated searches for(I=1;I<=N;I++)if(Mm[X][I]<Max&&Dis[I]<Dis[X]//Remember to determine the shortest path value at the same time must be sure that there is a path between the two points {Dp[X]+=Dfs(I); }returnDp[X];}intMain (void){intI,J,K,L,A,B;__int64C; while(~scanf("%d",&N) &&N)    {scanf("%i64d",&M);Memset(Mm,0,sizeof(Mm)); for(I=1;I<=N;I++) for(J=1;J<=N;J++)Mm[I][J]=Max; for(I=0;I<M;I++) {scanf("%d%d%i64d",&A,&B,&C);if(Mm[A][B]>C)//Because there is no problem, so I'm not sure if there is a heavy side, anyway, it is not wrong to writeMm[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]); }return0;}

Summary: The more write more smooth, to write memory search also more and more feel, feeling with sister handle, can't stop AH



Hdu--1142--a Walk Through The forest--deep search/dp/Shortest path/memory search

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.