• hdu1142 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
     
    这道题就是a点到终点的距离小于b到终点的距离,这样的前提下,
    问1到2有多少条路,就是最短路+深搜
    View Code
    #include<iostream>
    using namespace std;
    int n;//十字路口数
    int map[1001][1001];
    int a[1001],dp[1001];
    int mark[1001];
    void dijkstra(int start)//迪杰斯特拉算法
    {
        int i,j,min,k;
        
        for(i=1;i<=n;i++)
        {
            a[i] = map[i][start];
            mark[i] = 0;
        }
        a[start] = 0;
        mark[start] = 1;
        int num=1;
        
        while(num<n)
        {
            min = 2000000;
            for(j=1;j<=n;j++)
            {
                if(mark[j]==0 && a[j]<min)
                {
                    min = a[j];
                    k = j;
                }
            }
            if(min == 2000000)//注意 若没有 会在中间溢出
                break;
            mark[k] = 1;
            for(j=1;j<=n;j++)
            {
                if(mark[j]==0 && a[j]>a[k]+map[j][k])
                    a[j] = a[k]+map[j][k];
            }
            num++;
        }
    }
    
    int dfs(int v)//记忆法深搜
    {
        if(dp[v] != -1)
            return dp[v];
        if(v == 2)
            return 1;
        int i,temp,sum=0;
        for(i=1;i<=n;i++)
        {
            if(map[v][i]!=2000000 && a[v] > a[i])//有路相通而且要去的i点到终点站的距离要比v到终点站的距离小
            {
                temp = dfs(i);
                sum += temp;
            }
        }
        dp[v] = sum;
        return sum;
    }
    
    int main()
    {
        while(cin>>n && n)
        {
            int i,j,d,m;
            cin>>m;
            for(i=1;i<=n;i++)
            {
                dp[i] = -1;
                for(j=1;j<=n;j++)
                    map[i][j] = 2000000;
            }
            while(m--)
            {
                scanf("%d%d%d",&i,&j,&d);
                map[i][j] = map[j][i] = d;//无向图
            }
            //求出各点到终点站的最短距离
            dijkstra(2);//2为终点站
            
            dfs(1);//从1出发
            cout<<dp[1]<<endl;
        }
        return 0;
    }
     
  • 相关阅读:
    Typora的使用-规整笔记 让我以后的笔记内容更加整齐
    2019.11.8
    Java笔记1: 输入输出与变量常量
    2019.11.5 规整博客;整理分类;分章节发表博客
    2019.11.4 准备房子/研究汉诺塔问题和递归改变自我思维
    2013年小结
    legoblock秀上限
    阶段总结合计划
    《人月神话》读后感
    《JavaScript语言精髓与编程实践》读书笔记二
  • 原文地址:https://www.cnblogs.com/zlyblog/p/3036134.html
Copyright © 2020-2023  润新知