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中任何顶点的最短路径长度

(2)每个顶点对应一个距离值

S中顶点:从V0到此顶点的最短路径长度

T中顶点:从V0到此顶点的只包括S中顶点作中间

顶点的最短路径长度

话不多说,思想是基础,最重要的还是怎么样实现:我们就以这道题为例子:

代码:

#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f//不能换成0x7fffffff, 因为下面有个di[min_pos]+INF, 结果是会超INT范围的
#define MAXN 1007
int map[MAXN][MAXN], di[MAXN], ans[MAXN]; //map是邻接矩阵, di是存储的源点到各点的最小距离
bool vis[MAXN];//标记
int n;
void dijkstra(int v)
{
	int i, j;
	memset(vis, 0, sizeof(vis)); //初始化
	di[v] = 0;
	vis[v] = 1;
	for(i = 1; i <= n; i ++){
		if(!vis[i]){
			di[i] = map[v][i];
		}
	}
	for( i = 1; i < n; i ++){
		int min = INF;
		int min_pos = 0;//这个一定要等于0,不然的话会RE
		for(j = 1; j <= n; j ++){
			if(!vis[j]&&di[j] < min){
				min = di[j];
				min_pos = j;
			}
		}
		vis[min_pos] = 1;
		for(j = 1; j <= n; j ++){
			if(!vis[j]&&di[j] > map[min_pos][j]+di[min_pos]){
				di[j] = map[min_pos][j]+di[min_pos];
			}
		}
	}
}
int dfs(int v) //深搜:原理:从1点出发,每次一都找比当前di[v]小的点,找到二就说明有一条最短路径
{
	if(ans[v] != -1) return ans[v];  //记忆化搜索
	if(v == 2) return 1;//找到2就说名有一条路
	ans[v] = 0;  //初始化
	for(int i = 1; i <= n; i ++){
		if(map[v][i]!= INF&&di[i] < di[v]){  //map[v][i]!= INF 是为了判断v与i是不是有路
			ans[v] += dfs(i);
		}
	}
	return ans[v];
}
int main()
{
	int m, i, j, a, b, c;
	while(scanf("%d", &n), n){
		scanf("%d", &m);
		for(i = 1; i <= n; i ++){//初始化,map[i][i] = 0;map[i][j] = INF;
			for(j = 1; j <= n; j ++){
				map[i][j] = i==j?0:INF;
			}
		}
		for(i = 0; i < m; i ++){
			scanf("%d%d%d", &a, &b, &c);
			map[a][b] = map[b][a] = c;
		}
		dijkstra(2);
		//for(i = 1; i <= n; i ++){
		//	printf("%d..%d\n", di[i], i);
		//}
		memset(ans, -1, sizeof(ans));
		printf("%d\n", dfs(1));
	}
	return 0;
} 

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

时间: 2024-10-12 19:15:32

HDOJ 1142 A Walk Through the Forest 【Dijkstra】+【DFS】的相关文章

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

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

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

【I&#39;m Telling the Truth】【HDU - 3729】 【匈牙利算法,DFS】

思路 题意:该题主要说几个同学分别说出自己的名次所处区间,最后输出可能存在的未说谎的人数及对应的学生编号,而且要求字典序最大. 思路:刚刚接触匈牙利算法,了解的还不太清楚,附一个专门讲解匈牙利算法的博文,个人认为讲的比较清晰. AC代码 #include<iostream> #include<cstdio> #include<cstring> using namespace std; int T, n; struct Stue { int l, r; }; Stue p

Hdoj 1428 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): 6397 Accepted Submission(s): 2348 Problem Description Jimmy experiences a lot of stress at work these days, especially sin

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): 7601    Accepted Submission(s): 2796 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): 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,