HDU 1142 A Walk Through the Forest (Memory Search +dijkstra algorithm)

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): 7583 Accepted Submission (s): 2791


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

Sourceuniversity of Waterloo Local Contest 2005.09.24
Recommendeddy
original title Link:http://acm.hdu.edu.cn/showproblem.php?pid=1142
Test Instructions:Enter N,m, representing the number of vertices and the number of paths, followed by the M-line with three numbers representing the starting point, end point, and path length. Q: From ' 1 ' onwards, how many situations are there to reach ' 2 '. Path Requirements: If you want to reach B from point A, you must satisfy the shortest path length from point B to endpoint must be greater than the shortest path length of a to end point. Practice: Take ' 2 ' as the starting point, use the Dijkstra algorithm to find the shortest path length to other points, followed by the memory search.
AC Code:
#include <iostream> #include <cstring>using namespace std;const int Maxn=1005;int dis[maxn];int a[maxn][        Maxn];bool vis[maxn];int dp[maxn];const int inf=0x3f3f3f3f;int n,m;void dij (int x) {for (int i=1; i<=n; i++) {    Dis[i]=a[x][i];    } memset (Vis,false,sizeof (VIS));    Vis[x]=false;    dis[x]=0;    int p;        for (int i=1; i<n; i++) {int minn=inf;        for (int j=1; j<=n; J + +) {if (!vis[j]&&dis[j]<minn) minn=dis[p=j];        } if (Minn==inf) break;        Vis[p]=true; for (int j=1; j<=n; J + +) {if (!vis[j]&&dis[p]+a[p][j]<dis[j]) dis[j]=dis[p]+        A[P][J];    }}}int DFS (int x) {int sum=0;    if (dp[x]!=-1) return dp[x];    for (int i=1; i<=n; i++) {if (A[x][i]!=inf&&dis[x]>dis[i]) Sum+=dfs (i); } return dp[x]=sum;}        int main () {while (cin>>n,n) {cin>>m;    int x, y, Z;    memset (A,inf,sizeof (a));            while (m--) {cin>>x>>y>>z;        A[x][y]=a[y][x]=z;        } dij (2);        Memset (Dp,-1,sizeof (DP));        Dp[2]=1;    Cout<<dfs (1) <<endl; } return 0;}



HDU 1142 A Walk Through the Forest (Memory Search +dijkstra algorithm)

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.