0
点赞
收藏
分享

微信扫一扫

HDU 1142 A Walk Through the Forest 最短路+记忆搜索

郝春妮 2023-02-20 阅读 75


A Walk Through the Forest

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


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


/*
HDOJ 1142 最短路+记忆化搜索
开始没弄懂题意,就是A到终点距离大于B到终点距离
可以把终点出发,寻最短路,最后用这些每个点的最短路作为条件
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define MAX 1000000000
#define N 1001
int map[N][N],dis[N],vis[N],s[N];
int n,m;

void Dijkstra(int v0)
{
int i,j,k,min,loc;

memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
dis[i]=map[v0][i];//dis存储每点到终点的距离
dis[v0]=0;
vis[v0]=1;
for(i=1;i<=n;i++)
{
min=MAX;
loc=v0;
for(j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]<min)
{
loc=j;
min=dis[j];
}
}
if(min==MAX)
return ;
vis[loc]=1;
for(k=1;k<=n;k++)
if(!vis[k]&&map[loc][k]<MAX&&dis[loc]+map[loc][k]<dis[k])
dis[k]=map[loc][k]+dis[loc];
}
}

//记忆化搜索
int DFS(int v)
{
int i;
if(s[v])//如果该点已经访问过了,就返回到该点的路径数
return s[v];
if(v==2)//找到终点,返回1条路
return 1;
for(i=1;i<=n;i++)
{
if(map[v][i]<MAX&&dis[i]<dis[v])//满足V距离大于i距离
s[v]+=DFS(i);
}
return s[v];//返回到该点的所有路径数
}

int main()
{
int i,j,a,b,c;

// freopen("test.txt","r",stdin);
while(scanf("%d",&n),n)
{
scanf("%d",&m);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=MAX;

for(i=1;i<=m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(map[a][b]>c)
map[a][b]=map[b][a]=c;
}

Dijkstra(2);
memset(s,0,sizeof(s));
printf("%d\n",DFS(1));
}
return 0;
}




举报

相关推荐

0 条评论