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

A Walk Through the Forest

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

Problem Description

Jimmy experiences a lot ofstress at work these days, especially since his accident made workingdifficult. To relax after a hard day, he likes to walk home. To make thingseven nicer, his office is on one side of a forest, and his house is on theother. A nice walk through the forest, seeing the birds and chipmunks is quiteenjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. Healso wants to get home before dark, so he always takes a path to make progresstowards his house. He considers taking a path from A to B to be progress ifthere exists a route from B to his home that is shorter than any possible routefrom A. Calculate how many different routes through the forest Jimmy mighttake.

 

 

Input

Input contains several testcases followed by a line containing 0. Jimmy has numbered each intersection orjoining of paths starting with 1. His office is numbered 1, and his house isnumbered 2. The first line of each test case gives the number of intersectionsN, 1 < N ≤ 1000, and the number of paths M. The following M lines eachcontain a pair of intersections a b and an integer distance 1 ≤ d ≤ 1000000indicating a path of length d between intersection a and a differentintersection b. Jimmy may walk a path any direction he chooses. There is atmost one path between any pair of intersections.

 

 

Output

For each test case, output asingle 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节点的条件是b节点距离家比a节点距离家更近一些,那么我们就需要跑一个最短路求出各点到家的距离,显然,应该把家(即点2)作为起点。对于路径数的统计,我们可采用dfs,但根据题目所给的“You may assume that this number does not exceed 2147483647”,数据很大,直接dfs肯定会超时,故采用记忆化搜索。

PS:这道题最后采用了邻接表存每一条边,但我一开始是用数组保存的,但后来发现在记忆化搜索中需要判断两个点是否互相可达,直接用数组只能得到起点与其它点的距离,无法进行判断,而且其实用邻接表实现在时间上会有一定优化


#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
#define ll long long
const int maxn = 1005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
int n,m,cnt;
int dis[maxn];
int vis[maxn],dp[maxn];
int head[maxn];
struct node
{
    int t,w,next;
}e[maxn*maxn];

void add(int from,int to,int w)
{
    e[cnt].t=to;
    e[cnt].w=w;
    e[cnt].next=head[from];
    head[from]=cnt++;
}

void init()
{
    mst(head,-1);
    cnt=0;
}
void SPFA(int s)
{
    mst(dis,0x3f);
    mst(vis,0);
    queue<int> q;
    q.push(s);
    dis[s]=0;
    vis[s]=1;
    while(q.size())
    {
        int cur=q.front();
        q.pop();
        vis[cur]=0;
        for(int i=head[cur];i!=-1;i=e[i].next)
        {
            int u=e[i].t;
            int w=e[i].w;
            if(dis[u]>dis[cur]+w)
            {
                dis[u]=dis[cur]+w;
                if(vis[u]==0)
                {
                    q.push(u);
                    vis[u]=1;
                }
            }
        }
    }
}

int solve(int x)
{
    if(dp[x]==0)
    {
        for(int i=head[x];i!=-1;i=e[i].next)
        {
            int u=e[i].t;
            if(dis[u]<dis[x])
                dp[x]+=solve(u);
        }
        return dp[x];
    }
    return dp[x];
}
int main()
{
    int s,t,w;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%d",&m);
        init();
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&s,&t,&w);
            add(s,t,w);
            add(t,s,w);
        }
        SPFA(2);
        mst(dp,0);
        dp[2]=1;
        solve(1);
        printf("%d\n",dp[1]);
    }
    return 0;
}


今天在dalao的代码中发现了另一种实现边的储存的方法,用向量容器保存,我实现了一下,发现两者在时间上差不多,但后者在空间上实现了一定优化。

#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
#define ll long long
const int maxn = 1005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
int n,m,cnt;
int dis[maxn];
int vis[maxn],dp[maxn];
typedef struct edge
{
    int v,w;
}edge;
vector<edge> arr[maxn];
void SPFA(int s)
{
    mst(dis,0x3f);
    mst(vis,0);
    queue<int> q;
    q.push(s);
    dis[s]=0;
    vis[s]=1;
    while(q.size())
    {
        int cur=q.front();
        q.pop();
        vis[cur]=0;
        for(unsigned int i=0;i<arr[cur].size();i++)
        {
            int u=arr[cur][i].v;
            int w=arr[cur][i].w;
            if(dis[u]>dis[cur]+w)
            {
                dis[u]=dis[cur]+w;
                if(vis[u]==0)
                {
                    q.push(u);
                    vis[u]=1;
                }
            }
        }
    }
}

int solve(int x)
{
    if(dp[x]==0)
    {
        for(unsigned int i=0;i<arr[x].size();i++)
        {
            int u=arr[x][i].v;
            if(dis[u]<dis[x])
                dp[x]+=solve(u);
        }
        return dp[x];
    }
    return dp[x];
}
int main()
{
    int s,t,w;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<maxn;i++)
            arr[i].clear();
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&s,&t,&w);
            edge tmp;
            tmp.v=t;
            tmp.w=w;
            arr[s].push_back(tmp);
            tmp.v=s;
            arr[t].push_back(tmp);
        }
        SPFA(2);
        mst(dp,0);
        dp[2]=1;
        solve(1);
        printf("%d\n",dp[1]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值