zoukankan      html  css  js  c++  java
  • HDU1142 A Walk Through the Forest(最短路+DAG)

    A Walk Through the Forest
    
    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 9081    Accepted Submission(s): 3353
    
    
    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
    

    学到了DAG中如何记忆化(也可以按距离排序后处理)

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<cstring>
    #include<memory.h>
    #include<algorithm>
    #include<vector>
    #include<queue>
    using namespace std;
    const int maxn=1010;
    const int maxm=1000010;
    int Laxt[maxn];
    int len[maxm],To[maxm],Next[maxm],cnt,ans;
    int dis[maxn],used[maxn],p[maxn];
    queue<int>q;
    void _add(int u,int v,int d)
    {
        Next[++cnt]=Laxt[u];
        Laxt[u]=cnt;
        To[cnt]=v;
        len[cnt]=d;
    }
    void _dij(){
        while(!q.empty()){
            int u=q.front();q.pop();
            used[u]=0;
            for(int i=Laxt[u];i;i=Next[i]){
                if(dis[To[i]]>dis[u]+len[i]){
                    dis[To[i]]=dis[u]+len[i];
                    if(!used[To[i]])
                    q.push(To[i]);
                }
            }  
         }
    }
    int _find(int v)
    {
        if(p[v]) return p[v];
        int sum=0;//使用sum而不是直接p[v]加,保证了加完后才使用,保证了DAG的方向性。
        for(int i=Laxt[v];i;i=Next[i])
            if(dis[To[i]]<dis[v])
            sum+=_find(To[i]);
        p[v]=sum;
        return p[v];
    }
    int main()
    {
        int n,m,i,j,k,x,y,z;
        while(~scanf("%d",&n)){
            if(n==0) return 0;
            scanf("%d",&m);
            cnt=ans=0;
            memset(dis,0x7F,sizeof(dis));
            memset(Laxt,0,sizeof(Laxt));
            memset(used,0,sizeof(used));
            memset(p,0,sizeof(p));
            for(i=1;i<=m;i++){
                scanf("%d%d%d",&x,&y,&z);
                _add(x,y,z);
                _add(y,x,z);
            }
            p[2]=1;
            dis[2]=0;
            used[2]=1;
            q.push(2);
            _dij();       
            printf("%d
    ",_find(1));
        }
        return 0;
    }

     

  • 相关阅读:
    mysql5.5的安装与配置(亲测版)
    CentOS 6.5升级Python和安装IPython(亲测可用)
    运维mysql基础
    linux命令巧用,随手记
    《大话设计模式》——建造者模式
    《大话设计模式》——外观模式
    《大话设计模式》——模版方法模式
    抽象类和接口的区别
    《大话设计模式》——原型模式
    《大话设计模式》——工厂方法模式
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7632544.html
Copyright © 2011-2022 走看看