`
java-mans
  • 浏览: 11416896 次
文章分类
社区版块
存档分类
最新评论

hdu1142 A Walk Through the Forest

 
阅读更多

A Walk Through the Forest

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


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
24

大致题意:

他的办公室用1表示,家用2 表示,从1到2,中间可能会经过其它节点,而该节点可走的原则是:假设他此时在A处,B与其相邻,只有当B到2 路线中存在一条比A到2 的任意一条路径都短的路径,才能走B。问这样的路线有多少种?

分析:对于题目中的原则我可以这样考虑,如果B到2 的最短路径都比A到2 的最短路径长,显然题目的条件是不成立的,即B不能走。而如果B到2 的最短路径比A到2 的最短路径短,那么题目的条件是成立的,即这样的路线一定存在,路线长度也可能介于A,B分别到2 的最短路径长度之间。也就是说,我们需要求出每个点到2 的最短路径长度,然后按题目原则深搜,能搜出几条就是几条。

#include<stdio.h>
#include<string.h>
int map[1005][1005],used[1005],min[1005],m,n,ans,used2[1005],dp[1005];
void init()
{
	int i,j;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			if(i!=j) map[i][j]=999999999;
			else map[i][j]=0;
}
void find_short(int s)
{
	int i,j,mm,pos;  
    memset(used,0,sizeof(used));
    for(i=1;i<=n;i++)
		min[i]=map[s][i];
    min[s]=0;
    used[s]=1;
    for(i=2;i<=n;i++)
    {
		mm=999999999;
		for(j=1;j<=n;j++)
		{
			if(!used[j]&&mm>min[j])
			{
				pos=j;
				mm=min[j];
			}
		}
		if(mm==999999999) break;
		used[pos]=1;
		for(j=1;j<=n;j++)
		{
			if(!used[j]&&min[pos]+map[pos][j]<min[j])
				min[j]=min[pos]+map[pos][j];
		}
    }
}
int DFS(int num)
{
	int i;
	if(dp[num]!=-1) return dp[num];
	if(num==2) return 1;
	dp[num]=0;
	for(i=1;i<=n;i++)
		if(map[num][i]!=999999999&&min[num]>min[i])
		{
			dp[num]=dp[num]+DFS(i);
		}
		return dp[num];
}
int main()
{
	int i,x,y,z;
	while(scanf("%d",&n)&&n!=0)
	{
		scanf("%d",&m);
		init();
		for(i=1;i<=m;i++)
		{
			scanf("%d %d %d",&x,&y,&z);
			if(map[x][y]>z) map[x][y]=map[y][x]=z;
		}
		find_short(2);
		memset(dp,-1,sizeof(dp));
		ans=DFS(1);
		printf("%d\n",ans);
	}
	return 0;
}
/*参考了人家的代码才有了思路 */





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics