HDU1142 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): 7733    Accepted Submission(s): 2851

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

【分析】

题目意思是说一个人要从上班的地方回到家里,途中会经过一些地方,按下面规则问他回家会有几种路线:

题目已经定义好1为上班地方2为家,每个地点之间的距离都已经知道,哪么如果从A到B的一条路,可以走的条件

是B到2(家)的路程必须小于从A到2(家)的路程,其实就是A到家的最短路径必须大于B到家的最短路径。

默认一定可以走到家,也就是一定会有一种路线。最后是让你计算有几种路线。可以用迪杰斯特拉算法找一下所有点到终点的最短路,然后DFS搜一下就行了。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=1005;
int n;//十字路口数
int w[1001][1001];
int dist[1001],dp[1001];
int s[1001];
void dijkstra(int v)//迪杰斯特拉算法
{
    int i,j,mins,index;
    for(i=1; i<=n; i++)
    {
        dist[i] = w[i][v];
        s[i] = 0;
    }
    dist[v] = 0;
    s[v] = 1;
    for(i=1; i<n; i++)
    {
        mins = 2000000;
        for(j=1; j<=n; j++)
        {
            if(s[j]==0 && dist[j]<mins)
            {
                mins = dist[j];
                index = j;
            }
        }
        if(mins == 2000000)//注意 若没有 会在中间溢出
            break;
        s[index] = 1;
        for(j=1; j<=n; j++)
        {
            if(s[j]==0 && dist[j]>dist[index]+w[j][index])
                dist[j] = dist[index]+w[j][index];
        }
    }
}

int dfs(int v)//记忆法深搜
{
    if(dp[v] != -1)
        return dp[v];
    if(v == 2)
        return 1;
    int i,temp,sum=0;
    for(i=1; i<=n; i++)
    {
        if(w[v][i]!=2000000 && dist[v] > dist[i])//有路相通而且要去的i点到终点站的距离要比v到终点站的距离小
        {
            temp = dfs(i);
            sum += temp;
        }
    }
    dp[v] = sum;
    return sum;
}

int main()
{
    while(cin>>n && n)
    {
        int i,j,d,m;
        cin>>m;
        for(i=1; i<=n; i++)
        {
            dp[i] = -1;
            for(j=1; j<=n; j++)
                w[i][j] = 2000000;
        }
        while(m--)
        {
            scanf("%d%d%d",&i,&j,&d);
            w[i][j] = w[j][i] = d;//无向图
        }
//求出各点到终点站的最短距离
        dijkstra(2);//2为终点站
        dfs(1);//从1出发
        cout<<dp[1]<<endl;
    }
    return 0;
}

时间: 2024-12-09 09:55:54

HDU1142 A Walk Through the Forest(dijkstra)的相关文章

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 - 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

UVA 10917 Walk Through the Forest(Dijkstra+DAG动态规划)

题意:gbn最近打算穿过一个森林,但是他比较傲娇,于是他决定只走一些特殊的道路,他打算只沿着满足如下条件的(A,B)道路走:存在一条从B出发回家的路,比所有从A出发回家的路径都短.你的任务是计算一共有多少条不同的回家路径.其中起点的编号为1,终点的编号为2. 思路:首先从终点Dijkstra一次,求出每个点u回家的最短路长度,那么相当于创建了一个新图,当d[B]<d[A]时有一条A指向B的有向边,则题目的目标就是求出起点到终点的路径条数.因为这个图是DAG,可以用动态规划求解. #include

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中任何顶点

HDU1142 A Walk Through the Forest 【SPFA】+【记忆化搜索】

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5688    Accepted Submission(s): 2089 Problem Description Jimmy experiences a lot of stress at work these days, especial

最短路径算法——迪杰斯特拉算法(Dijkstra)

图结构中应用的最多的就是最短路径的查找了,关于最短路径查找的算法主要有两种:迪杰斯特拉算法(Dijkstra)和Floyd算法. 其中迪杰斯特拉算法(Dijkstra)实现如下: 原理就是不断寻找当前的最优解: void main() { int V[Max][Max]={0,8,32,Infinity,Infinity, 12,0,16,15,Infinity, Infinity,29,0,Infinity,13, Infinity,21,Infinity,0,7, Infinity,Infi

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

poj1062昂贵的聘礼(Dijkstra**)

1 /* 2 题意: 物主有一个物品,价值为P,地位为L, 以及一系列的替代品Ti和该替代品所对应的"优惠"Vi 3 g[u][i] 表示的是u物品被i物品替换后的优惠价格!(u>0, i>0) 4 g[u][0]表示不用替换该物品的实际价格 ! 5 d[0]表示的是第一个物品经过一系列的物品替换之后的最少优惠价格! 6 7 思路:每当我们通过Dijkstra算法得到离源点(1)最近的距离的节点 p的时候(也就是1...pre[p], p)这条 8 路径上的物品互相替换后得

迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合(用S表示,初始时S中只有一个源点,以后每求得一条最短路径 , 就将 加入到集合S中,直到全部顶点都加入到S中,算法就结束了),第二组为其余未确定最短路径的顶点集合(用U表示),按最短路径长度的递增次序依次把第二组的顶点加入S中.在加入的过程中,总保持从源点v到S中各顶点的最短路径长度不大于从源点v