HDU ACM 1142 A Walk Through the Forest->SPFA算法+记忆化深搜

分析:找到吉米从办公室穿过森林回到家(也就是从点1到点2)的最短路径有多少条,其中要满足如果要走A到B这条路,那么就有从A到终点的距离都大于B到终点的距离。

解法:spfa算法+记忆化深搜

1、spfa求出从终点2到其他所有点的最短路

2、记忆化DFS从1开始向其他点深搜,最后结果就是dp[1]。

#include<iostream>
#include<queue>
using namespace std;  

int u[2000002];
int v[2000002];
int w[2000002];
bool vis[1001];
int d[1001];
int first[1001];
int Next[2000002];
int dp[1001];

void Init(int n,int m)
{
    int i;  

    for(i=1;i<=n;i++)
    {
        vis[i]=false;
        first[i]=-1;
		dp[i]=-1;
    }
    for(i=0;i<m;i++)
        Next[i]=-1;
}

int DFS(int u)
{
	int i,sum;

	if(dp[u]!=-1) return dp[u];   //记忆化
	if(u==2) return 1;           //终点

	sum=0;
	for(i=first[u];i!=-1;i=Next[i])
	{
		if(d[u]>d[v[i]])
			sum+=DFS(v[i]);
	}
	return dp[u]=sum;
}

void spfa(int n,int s)
{
    queue<int> q;
    int i,x,y;  

    for(i=1;i<=n;i++)
        d[i]=(i==s)?0:0x7fffffff;   //初始化,自己到自己为0,其他到自己相当于无穷大  

    q.push(s);
    while(!q.empty())
    {
        x=q.front();
        q.pop();
        vis[x]=false;
        for(i=first[x];i!=-1;i=Next[i])
        {
            y=v[i];
            if(d[y]>d[x]+w[i])
            {
                d[y]=d[x]+w[i];
                if(!vis[y])
                {
                    vis[y]=true;
                    q.push(y);
                }
            }
        }
    }
}  

void Read(int m)
{
    int i,a,b;  

    for(i=0;i<m;i++)
    {
        scanf("%d %d %d",&a,&b,&w[i]);
        u[i]=a;                        //存储正向边,因为是无向图
        v[i]=b;
        Next[i]=first[a];
        first[a]=i;  

        w[i+1]=w[i];                //存储反向边
        i++;
        u[i]=b;
        v[i]=a;
        Next[i]=first[b];
        first[b]=i;
    }
}

int main()
{
    int n,m;  

    while(scanf("%d",&n)==1 && n)
    {
		scanf("%d",&m);
        Init(n,m+m);  //m+m是因为无向边,每条都要保存两条
        Read(m+m);
        spfa(n,2);     //搜出2到其他所有点的最短路
		cout<<DFS(1)<<endl;  //从1开始深搜,最终即为结果
    }
    return 0;
}
时间: 2024-10-13 22:48:26

HDU ACM 1142 A Walk Through the Forest->SPFA算法+记忆化深搜的相关文章

hdu 1142 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): 5984    Accepted Submission(s): 2211 Problem Description Jimmy experiences a lot of stress at work these days, especial

hdu 1342 记忆化深搜

Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1657    Accepted Submission(s): 811 Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2,...,49

HDU 1142 A Walk Through the Forest(dijkstra+记忆化DFS)

题意: 给你一个图,找最短路.但是有个非一般的的条件:如果a,b之间有路,且你选择要走这条路,那么必须保证a到终点的所有路都小于b到终点的一条路.问满足这样的路径条数 有多少,噶呜~~题意是搜了解题报告才明白的Orz....英语渣~ 思路: 1.1为起点,2为终点,因为要走ab路时,必须保证那个条件,所以从终点开始使用单源最短路Dijkstra算法,得到每个点到终点的最短路,保存在dis[]数组中. 2.然后从起点开始深搜每条路,看看满足题意的路径有多少条. 3.这样搜索之后,dp[1]就是从起

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】

题意:从2到1的所有路径中找出最短的路,并且输出最短路径有几条. 策略:先求出最短路径,然后再找出从2到1的最短路径有几条.最短路径用dijkstra算法来求出,什么是dijkstra算法,简单来说,dijkstra算法就是路径长度递增次序产生最短路径的算法: 基本思想是:把集合V分成两组: (1)S:已求出最短路径的顶点的集合 (2)V-S=T:尚未确定最短路径的顶点集合 将T中顶点按最短路径递增的次序加入到S中, 保证:(1)从源点V0到S中各顶点的最短路径长度都不大于 从V0到T中任何顶点

HDU 5024 Wang Xifeng&#39;s Little Plot (枚举 + DFS记忆化搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 513    Accepted Submission(s): 338 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

HDU 1142 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): 6350    Accepted Submission(s): 2332 Problem Description Jimmy experiences a lot of stress at work these days, especial

HDU 1142 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): 7583    Accepted Submission(s): 2791 Problem Description Jimmy experiences a lot of stress at work these days, especial

题解报告:hdu 1142 A Walk Through the Forest

题目链接:acm.hdu.edu.cn/showproblem.php?pid=1142 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,

HDU - 1142 A Walk Through the Forest(Dijkstra+DFS)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题意:从1到达2,路径要尽可能短(就先一遍dijkstra),并且要求每次距离2的路径要比上一个点距离2的路径近,求有多少符合条件的线路. 以2为起始点先进行一遍dijkstra,然后再从1开始dfs,寻找符合条件的线路(就是可以搜到2的线路). #include <bits/stdc++.h> using namespace std; const int INF=0x3f3f3f3f; c