HDOJ1142 A Walk Through the Forest

来源:互联网 发布:淘宝花呗卖家开通条件 编辑:程序博客网 时间:2024/06/04 18:25

A Walk Through the Forest

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


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 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10
 

Sample Output
24
 

Source
University of Waterloo Local Contest 2005.09.24
 

Recommend
Eddy   |   We have carefully selected several similar problems for you:  1162 1596 1301 1598 1690 

单源最短路径用dijstra计算,然后用记忆化搜索求解方案数
#include <iostream>#include <cmath>#include <ctime>#include <cstring>using namespace std;const int maxn = 1005;const int inf = 1<<30;int n,m,i,j,k;int a[maxn][maxn],dp[maxn],dis[maxn];bool used[maxn];void input(){    cin >> m;    int x,y,d;    for (i=1; i<=m; i++) {        cin >> x >> y >> d;        a[x][y] = a[y][x] = d;    }}void init(){    for (i=1; i<=n; i++) {        used[i] = 0;        dis[i] = inf;    }    for (i=1; i<=n; i++)        for (j=1; j<=n; j++)          a[i][j] = -1;    for (i=1; i<=n; i++) dp[i] = -1;}void dijstra(){    //因为之后要记忆化搜索,dfs递归的形式搜索,所以一开始把终点2当做出发点    used[2] = 1;      dis[2] = 0;    int s = 2,min_dis;    for (j=1; j<=n; j++){        for (i=1; i<=n; i++) if (a[s][i]!=-1) dis[i] = min (dis[i], dis[s] + a[s][i]);        min_dis = inf;        for (i=1; i<=n; i++) if (!used[i] && min_dis>dis[i]){            min_dis = dis[i];            s = i;        }        used[s] = 1;    }}int dfs(int x){    int i;    if (dp[x]!=-1) return dp[x];    if (x==2) return 1;    dp[x] = 0;    for (i=1; i<=n; i++) if (a[x][i]!=-1 && dis[i] < dis[x]) dp[x] += dfs(i); // 要保证点x到点i是连通的,所以a[x][i]!=-1    return dp[x];}int main(){    std::ios::sync_with_stdio(false);    while(cin >> n && n){        init();        input();        dijstra();        cout << dfs(1) << endl;    }    return 0;}


原创粉丝点击