HDU 1142 A Walk Through the Forest(spfa最短路+dfs记忆化搜索)

来源:互联网 发布:用vue.js做出来的网页 编辑:程序博客网 时间:2024/05/17 00:18
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
 
开始邻接矩阵实现 后来又写了邻接表实现,都能A,都贴上

//Must so#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<ctype.h>#include<queue>#include<vector>#include<set>#include<cstdio>#include<cmath>#define mem(a,x) memset(a,x,sizeof(a))#define inf 1<<29#define NN 1003using namespace std;const double PI = acos(-1.0);typedef long long LL;/**********************************************************************强行将dfs与spfa结合起来了因为题目要求,就算A-B是连通的,能走还必须看A到终点的最短路是否大于B到终点的最短路,所以必须求最短路,再深搜有多少条路**********************************************************************/struct Node{    int v,w,next;}e[1000000];//我去,题目没说有多少条边怎么办?int h[NN];int d[NN];queue<int>q;bool vis[NN];int n,m;void spfa(){    mem(vis,0);    for (int i = 0;i <= n;i++)    {        d[i] = inf;    }    q.push(2);//源点是终点2    vis[2] = 1;    d[2] = 0;    while (!q.empty())    {        int x = q.front();        q.pop();        vis[x] = 0;        for (int i = h[x];i != -1;i = e[i].next)        {            if (d[e[i].v] > d[x] + e[i].w)            {                d[e[i].v] = d[x] + e[i].w;                if (vis[e[i].v] == 0)                {                    q.push(e[i].v);                    vis[e[i].v] = 1;                }            }        }    }}int dp[NN];//dp[i]表示i点的路径数int dfs(int x)//记忆化搜索,返回该点的路径数{    if (dp[x]) return dp[x];    for (int i = h[x];i != -1;i = e[i].next)    {        if (d[x] > d[e[i].v])        {            dp[x] += dfs(e[i].v);        }    }    return dp[x];}int main(){    while (cin>>n)    {        if (n == 0) break;        cin>>m;        mem(h,-1);        mem(dp,0);        dp[2] = 1;        int o = 0;        for (int i = 0,uu,vv,ww;i < m;i++)        {            scanf("%d%d%d",&uu,&vv,&ww);            e[o].v = vv;            e[o].w = ww;            e[o].next = h[uu];            h[uu] = o++;            e[o].v = uu;            e[o].w = ww;            e[o].next = h[vv];            h[vv] = o++;        }        spfa();        printf("%d\n",dfs(1));    }    return 0;}

//Must so#include<iostream>#include<algorithm>#include<string>#include<cstring>#include<ctype.h>#include<queue>#include<vector>#include<set>#include<cstdio>#include<cmath>#define mem(a,x) memset(a,x,sizeof(a))#define inf 1<<29#define NN 1003using namespace std;const double PI = acos(-1.0);typedef long long LL;/**********************************************************************强行将dfs与spfa结合起来了因为题目要求,就算A-B是连通的,能走还必须看A到终点的最短路是否大于B到终点的最短路,所以必须求最短路,再深搜有多少条路本来用邻接表的,考虑到题目没给边的范围,最后还是用的二维邻接矩阵**********************************************************************/int mp[NN][NN];int d[NN];vector<int>u[NN];//u[i]记录与i相连的点queue<int>q;bool vis[NN];int n,m;int dp[NN];//dp[i]表示i点的路径数void init(){    for (int i = 0;i <= n;i++)    {        dp[i] = 0;        u[i].clear();        for (int j = 0;j <= n;j++)        {            mp[i][j] = inf;        }        mp[i][i] = 0;    }}void spfa(){    mem(vis,0);    for (int i = 0; i <= n; i++)    {        d[i] = inf;    }    q.push(2);//源点是终点2    vis[2] = 1;    d[2] = 0;    while (!q.empty())    {        int x = q.front();        q.pop();        vis[x] = 0;        for (int i = 1; i <= n; i++)        {            if (d[i] > d[x] + mp[x][i])            {                d[i] = d[x] + mp[x][i];                if (vis[i] == 0)                {                    q.push(i);                    vis[i] = 1;                }            }        }    }}int dfs(int x)//记忆化搜索,返回该点的路径数{    if (dp[x]) return dp[x];    for (int i = 0;i < u[x].size();i++)    {        if (d[x] > d[u[x][i]])        {            dp[x] += dfs(u[x][i]);        }    }    return dp[x];}int main(){    while (cin>>n)    {        if (n == 0) break;        cin>>m;        init();        dp[2] = 1;        for (int i = 0,uu,vv,ww; i < m; i++)        {            scanf("%d%d%d",&uu,&vv,&ww);            if (ww < mp[uu][vv])            {                mp[uu][vv] = ww,mp[vv][uu] = ww;                u[uu].push_back(vv);                u[vv].push_back(uu);            }        }        spfa();        printf("%d\n",dfs(1));    }    return 0;}


1 0
原创粉丝点击