zoukankan      html  css  js  c++  java
  • [题解] hdu 1142 A Walk Through the Forest (dijkstra最短路 + 记忆化搜索)

    - 传送门 -

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

    # A Walk Through the Forest

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

    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

    [Statistic](http://acm.hdu.edu.cn/statistic.php?pid=1142) | [Submit](http://acm.hdu.edu.cn/submit.php?pid=1142) | [Discuss](http://acm.hdu.edu.cn/discuss/problem/list.php?problemid=1142) | [Note](http://acm.hdu.edu.cn/note/note.php?pid=1142)

    - 题意 -

     定义可走路径为((x o y)), (y) 到终点的最短距离 < (x) 到终点的最短距离.
     问只能走可走路径的情况下,从起点到达终点共有几种走法.
     

    - 思路 -

     先反向最短路求出每个点到终点的最短距离.
     再搜索可走路径.
     注意从某一个点到终点的走法数量是一定的, 但我们可能多次搜到同一个点, 这就浪费了时间, 所以要采用记忆化搜索.
     
     细节见代码.
     

    - 代码 -

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    
    const int N = 1e3 + 5;
    const int inf = 0x3f3f3f3f;
    
    int VIS[N], DIS[N], MAP[N][N], DP[N];
    int n, m;
    
    struct dij {
    	int x, d;
    	dij(int a, int b) {x = a, d = b;}
    	bool operator < (const dij &tmp) const {
    		return d > tmp.d;
    	}
    };
    
    void dijkstra(int x) {
    	priority_queue<dij>Q;
    	memset(VIS, 0, sizeof (VIS));
    	memset(DIS, 0x3f, sizeof (DIS));
    	DIS[x] = 0;
    	dij tmp(x, DIS[x]);
    	Q.push(tmp);
    	while (!Q.empty()) {
    		dij x = Q.top();
    		Q.pop();
    		int u = x.x;
    		if (VIS[u]) continue;
    		VIS[u] = 1;
    		for (int i = 1; i <= n; ++i)
    			if (!VIS[i] && DIS[i] > DIS[u] + MAP[u][i]) {
    				DIS[i] = DIS[u] + MAP[u][i];
    				dij y(i, DIS[i]);
    				Q.push(y);
    			}
    	}
    }
    
    int DFS(int x) {
    	if (DP[x]) return DP[x]; //记忆化搜索
    	int ans = 0;
    	for (int i = 1; i <= n; ++i)
    		if (MAP[i][x] != inf && DIS[i] < DIS[x])
    			ans += DFS(i);
    	DP[x] = ans;
    	return ans;
    }
    
    int main() {
    	while (~scanf("%d", &n) && n) {
    		scanf("%d", &m);
    		for (int i = 1; i <= n; ++i)
    			for (int j = 1; j <= n; ++j)
    				if (i == j) MAP[i][j] = 0;
    				else MAP[i][j] = inf;
    		for (int i = 1; i <= m; ++i) {
    			int x, y, v;
    			scanf("%d%d%d", &x, &y, &v);
    			MAP[x][y] = MAP[y][x] = v;
    		}
    		dijkstra(2);
    		memset(DP, 0, sizeof (DP));
    		DP[2] = 1;
    		printf("%d
    ", DFS(1));
    	}
    	return 0;
    }
    
  • 相关阅读:
    laravel 资源控制器方法列表
    laravel 用户认证简单示例
    使用Faker库生成模拟数据
    js获取世界不同时区的当前时间
    html2canvas将页面内容生成图片
    canvas绘制环形进度条
    H5 实现图片上传预览
    js实现复制内容到粘贴板
    node.js创建简单服务测试请求数据
    ES6语法知识
  • 原文地址:https://www.cnblogs.com/Anding-16/p/7395903.html
Copyright © 2011-2022 走看看