A Walk Through the Forest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2183 Accepted Submission(s): 787
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
题目大意:寻找一共有多少条符合题意的路。能够从点A走到点B的要求是:点A到终点的最短路 > 点B到终点的最短路。 也就是说:从终点出发,求每一个点的最短路,然后那些最短路的值记录起来,作为能否通过的判断条件。最后用记忆化搜索来搜索出一共多少条符合要求的路。普通的dfs是超时的,bfs是超内存的。
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142
代码:
#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <queue>
#include <vector>
using namespace std;
const int INF = 99999999;
const int N = 1005;
vector<int> road[N]; //定义一个向量(第一次用-O(∩_∩)O-)
int map[N][N], dist[N], s[N];
bool visit[N];
int n, t, sum;
void init()
{
int i, j;
sum = 0;
memset(s, 0, sizeof(s));
for(i = 0; i <= n; i++) road[i].clear();
for(i = 0; i <= n; i++)
for(j = 0; j <= n; j++)
if(i == j) map[i][j] = 0;
else map[i][j] = INF;
}
void input()
{
int a, b, d;
while(t--)
{
scanf("%d %d %d", &a, &b, &d);
if(d < map[a][b])
{
map[a][b] = map[b][a] = d; //双向的
road[a].push_back(b);
road[b].push_back(a);
}
}
}
void spfa() //比较喜欢用spfa
{
int i, now;
memset(visit, false, sizeof(visit));
for(i = 1; i <= n; i++) dist[i] = INF;
dist[2] = 0;
queue<int> Q;
Q.push(2);
visit[2] = true;
while(!Q.empty())
{
now = Q.front();
Q.pop();
visit[now] = false;
for(i = 1; i <= n; i++)
{
if(dist[i] > dist[now] + map[now][i])
{
dist[i] = dist[now] + map[now][i];
if(visit[i] == false)
{
Q.push(i);
visit[i] = true;
}
}
}
}
}
int dfs(int now) //记忆化搜索
{
int i;
if(now == 2) return 1; //找到终点,返回1(1条路)
if(s[now]) return s[now]; //该点已经走过,返回:过该点有多少条路
for(i = 0; i < road[now].size(); i++)
{
if(dist[now] > dist[ road[now][i] ])
{
s[now] += dfs(road[now][i]);
}
}
return s[now]; //返回:该点一共可以有多少条路
}
int main()
{
while(scanf("%d", &n), n)
{
scanf("%d", &t);
init();
input();
spfa();
sum = dfs(1); //sum一共多少条路,sum = s[1] 也行
printf("%d\n", sum);
}
return 0;
}
分享到:
相关推荐
最短路(HDU-2544).rar
acm hdu as easy as a+b
搜索 dfs 解题代码 hdu1241
八数码的A*算法~不是很高效,但是很适合刚刚学这个算法的朋友们
Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。 这时他已经不是那个只会做100以内加法的那个“小明”了,...请在一行里面输出输出A+B的值,请输出最简形式。
HDU最全ac代码
本人准备2020年保研机试时刷的题目(虽然最后机试取消了,...来自某中流985,在HDU和vjudge平台上大概刷了400道。本文件地图(excel表格)包含了绝大部分我刷过的题目,笔记中具有思路、代码、总结和心得。 大佬勿入!
hdu 1695 GCD(欧拉函数+容斥原理).docx
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
ACM题库,一些题目和答案,以及解题报告,传上来共享
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
hdu 1574 passed sorce
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
HDU的一题........HDU DP动态规