zoukankan      html  css  js  c++  java
  • hdu1142 dij+记忆化深搜

    A Walk Through the Forest

    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. 
     
    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 6
    1 3 2
    1 4 2
    3 4 3
    1 5 12
    4 2 34
    5 2 24
    7 8
    1 3 1
    1 4 1
    3 7 1
    7 4 1
    7 5 1
    6 7 1
    5 2 1
    6 2 1
    0
     
    Sample Output
    2 4
     

    题意:本题要求S->E的路径总数,还有一个条件就是a->b有路的情况,只有当d[a]>d[b]才会存在,d[a]表示a点到E的最短路径
    刚开始也是去求最小路径的数量,后来才理解题意,但是还是要用dij初始化所有点到E的单源最短路径,dfs取边时,加上d[a]>d[b]的条件就可以了,本题数据较大,应该采用记忆化dfs
    以前做dp的时候,还是没有深刻理解记忆化dfs的意义,只要某一节点已经被访问过了,则再次dfs到该节点时,就可以直接取该节点的值
    技巧:一开始,应该将所有的节点标记为-1,遍历时mark为0,这样无论该节点是否为最后路径上的点,都表示该点(以及它的后继节点)已经被访问过了
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <stack>
    #include <vector>
    #include <algorithm>
    #include <queue>
    const int inf = 0x3f3f3f;
    const int MAXN = 1e3+10;
    const int MAXNN = 1e6+10;
    
    using namespace std;
    
    struct heapnode{
        //int st;
        int dist;
        int st;
        bool operator < (const heapnode& rhs) const{
            return dist>rhs.dist;
        }
    };
    
    struct edge{
        int st;
        int to;
        int dist;
        int next;
    };
    
    edge e[2*MAXNN];
    int first[MAXN];
    int top;
    int p[2*MAXNN]; //记录上一条边
    int d[MAXN];    //s到各节点的距离
    int done[MAXN]; //是否已经被永久标记
    int num[MAXN];
    int n,m;
    //int st,ed;
    
    void init(){
        memset(first,-1,sizeof(first));
        memset(num,-1,sizeof(num));
        top = 0;
    }
    
    void addedge(int u,int v,int dist){
        e[top].st = u;
        e[top].to = v;
        e[top].next = first[u];
        e[top].dist = dist;
        first[u] = top++;
    }
    
    void dijkstra(int s){
        heapnode a;
        priority_queue<heapnode> Q;
        for(int i=0;i<n;i++){
            d[i] = inf;
        }
        d[s] = 0;
       // num[s] = 1;
        memset(done,0,sizeof(done));
        //Q.push((heapnode){0,s});
        a.dist = 0;
        a.st = s;
        Q.push(a);
        while(!Q.empty()){
            heapnode x = Q.top();
            Q.pop();
            int u = x.st;
            if(done[u])continue;
            done[u] = 1;
    
            for(int i=first[u];i!=-1;i=e[i].next){
                if(d[e[i].to]>d[u]+e[i].dist){
                    d[e[i].to] = d[u]+e[i].dist;
                    p[e[i].to] = i;
                    //Q.push((heapnode){d[e[i].to],e[i].to});
                    a.dist = d[e[i].to];
                    a.st = e[i].to;
                    Q.push(a);
                    //num[e[i].to] = num[u];
                }
                //else if(d[e[i].to]==d[u]+e[i].dist){
                    //num[e[i].to]+= num[u];
               // }
            }
        }
    }
    
    //记忆化dfs
    int dfs(int u){
        //if(u==1)return 1;
        if(num[u]!=-1)return num[u];    //已经访问过的节点
        num[u] = 0;
        int v;
        for(int i=first[u];i!=-1;i=e[i].next){
            v = e[i].to;
            if(d[u]>d[v]){
                num[u] += dfs(v);
            }
        }
        return num[u];
    }
    
    
    int main()
    {
        int u,v,a;
        while(scanf("%d",&n),n){
            scanf("%d",&m);
            init();
            for(int i=0;i<m;i++){
                scanf("%d%d%d",&u,&v,&a);
                u--;
                v--;
                addedge(u,v,a);
                addedge(v,u,a);
            }
            dijkstra(1);
            num[1] = 1;
            dfs(0);
            cout<<num[0]<<endl;
        }
        return 0;
    }
    View Code
     
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    document.getElementById()使用方法
    Delphi XE7 发布时间
    JavaScript动态更改页面元素
    TCP/IP-协议族----17、应用层简单
    查看员工信息每个部门的最低工资
    VB6.0“挑衅”.NET!
    MapReduce计数器
    Linux学习记录--命名管道通信
    cocos2d-x V3.0 呼叫加速度计 Acceleration
    Linux Kernel(Android) 加密算法汇总(四)-应用程序调用OpenSSL加密演算法
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5475718.html
Copyright © 2011-2022 走看看