HDU 1142 A Walk through the Forest

Source: Internet
Author: User

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=1142

Topic:

A Walk through the Forest
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3689 accepted Submission (s): 1340

Problem Description
Jimmy experiences a lot of stress at work this days, especially since his accident made G difficult. To relax on a hard day, he likes to walk home. To make things even nicer, he office is on one side of a forest, and he is 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. 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 being progress if there exists a route from B to his home this is shorter than any Possible route from A. Calculate many different routes the through Jimmy Forest might.

Input
Input contains several test cases followed by a line containing 0. Jimmy has numbered each intersection or joining of paths with 1. His office is numbered 1, and he is numbered 2. The ' a ' 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 a 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 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 the does not exceed 2147483647

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Sample Input

5 6
1 3 2
1
4 2 3 4 3 1 5 4 2 5 2 7 8 1 3 1 1 4 1 3 7 1 7 4 7 5 1 6 7 1 5 2 1 6 2 1-
0

Sample Output

2
4

Analysis and Summary:

To do this problem is really twists, a full WA has 30+ times.

1. The topic meaning understanding error. A passage in the title is not well understood:

He considers taking a path from a to B to being progress if there exists a route from B to his home this is shorter than any Possible route from A.

This sentence means that for two o ' a,b, if point B arrives at home with the shortest distance less than point A to reach the home shortest distance, then point A can reach point B.

And I understood the problem as the number of different short circuits in the office to the home.

2. After resolving the above error, we can then make every point to reach Point 2 (home) of the shortest path, and then according to the minimum number of points to find out how many paths to the home.

If the direct search is timed out, use a memory search.

3. Because of their carelessness, because my program has two arrays named D and DP, in the write memory words to search the two arrays confused, the result WA more than 10 times and found no reason.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <utility  
    
> #include <string> #include <map> using namespace std;  
typedef int TYPE;  
typedef pair<type,int>pii;  
Const Type INF = 0x7fffffff;  
const int VN = 1005;  
    
const int EN = VN*VN;  
int n,m,size;  
int HEAD[VN];  
Type D[VN];  
BOOL G[VN][VN];  
BOOL VIS[VN];  
int cnt;  
    
int DP[VN];  
    struct edge{int v,next;  
Type W;  
    
}e[en];  
    void Init () {size=0;  
    cnt=0;  
    memset (g, 0, sizeof (g));  
    Memset (Head,-1, sizeof (head));  
Memset (DP,-1, sizeof (DP));  
    } void Addedge (int u,int v,type W) {e[size].v=v;  
    E[size].w=w;  
    E[size].next=head[u];  
head[u]=size++;  
    } void Dijkstra (int src) {for (int i=1; i<=n; ++i) D[i]=inf;  
    D[SRC] = 0;  
    Priority_queue<pii,vector<pii>,greater<pii> >q;  
    Q.push (Make_pair (D[SRC],SRC)); while (!Q.empty ()) {PII x = Q.top (); Q.pop ();  
        int u = x.second;  
        if (D[u]!= x.first) continue;  
            for (int e=head[u]; E!=-1 e=e[e].next) {Type tmp=d[u]+e[e].w;  
                if (D[E[E].V] > tmp) {D[E[E].V] = tmp;  
            Q.push (Make_pair (TMP, E[E].V));  
    int dfs (int u) {if (dp[u]!=-1) return dp[u];  
    if (u==2) return 1;  
    int cnt=0;  
    for (int v=1; v<=n; ++v) if (D[v]<d[u] && g[u][v]) {cnt + = DFS (v);  
    } Dp[u] = cnt;  
return Dp[u];  
    int main () {int u,v;  
    Type W;  
        while (~SCANF ("%d", &n) &n) {scanf ("%d", &m);  
        Init ();  
            for (int i=0; i<m; ++i) {scanf ("%d%d%d", &u,&v,&w);  
            G[U][V] = G[v][u] = 1;  
            Addedge (U,V,W);  
        Addedge (V,U,W);  
        } Dijkstra (2);  printf ("%d\n", DFS (1));
    return 0; }

Author: csdn Blog shuangde800

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.