zoukankan      html  css  js  c++  java
  • HDU1142 A Walk Through the Forest(dijkstra)

    A Walk Through the Forest

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 7733    Accepted Submission(s): 2851

    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
    【分析】

    题目意思是说一个人要从上班的地方回到家里,途中会经过一些地方,按下面规则问他回家会有几种路线:

    题目已经定义好1为上班地方2为家,每个地点之间的距离都已经知道,哪么如果从A到B的一条路,可以走的条件

    是B到2(家)的路程必须小于从A到2(家)的路程,其实就是A到家的最短路径必须大于B到家的最短路径。

    默认一定可以走到家,也就是一定会有一种路线。最后是让你计算有几种路线。可以用迪杰斯特拉算法找一下所有点到终点的最短路,然后DFS搜一下就行了。

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pi acos(-1.0)
    using namespace std;
    typedef long long ll;
    const int N=1005;
    int n;//十字路口数
    int w[1001][1001];
    int dist[1001],dp[1001];
    int s[1001];
    void dijkstra(int v)//迪杰斯特拉算法
    {
        int i,j,mins,index;
        for(i=1; i<=n; i++)
        {
            dist[i] = w[i][v];
            s[i] = 0;
        }
        dist[v] = 0;
        s[v] = 1;
        for(i=1; i<n; i++)
        {
            mins = 2000000;
            for(j=1; j<=n; j++)
            {
                if(s[j]==0 && dist[j]<mins)
                {
                    mins = dist[j];
                    index = j;
                }
            }
            if(mins == 2000000)//注意 若没有 会在中间溢出
                break;
            s[index] = 1;
            for(j=1; j<=n; j++)
            {
                if(s[j]==0 && dist[j]>dist[index]+w[j][index])
                    dist[j] = dist[index]+w[j][index];
            }
        }
    }
    
    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(w[v][i]!=2000000 && dist[v] > dist[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++)
                    w[i][j] = 2000000;
            }
            while(m--)
            {
                scanf("%d%d%d",&i,&j,&d);
                w[i][j] = w[j][i] = d;//无向图
            }
    //求出各点到终点站的最短距离
            dijkstra(2);//2为终点站
            dfs(1);//从1出发
            cout<<dp[1]<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    redis-3.2.5 make 报错
    haproxy 实现多域名证书https
    centos7修改主机名
    ngx_image_thumb模块生成缩略图
    查看nginx在安装时开启了哪些模块
    C# install-package:"xx"已拥有为“xxx”定义的依赖项
    JetBrains 2017/2018全系列产品激活工具
    查看win10版本方法,及win10升级方法
    Windows 10正式版的历史版本
    open '/dev/hwlog_switch' fail -1, 13. Permission denied
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/5723429.html
Copyright © 2011-2022 走看看