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;
}
/*参考了人家的代码才有了思路 */
分享到:
相关推荐
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
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...
杭电ACMhdu1163
HDU1059的代码
For a positive integer n, let’s denote function f(n,m) as the m-th smallest integer x that x>n and gcd(x,n)=1. For example, f(5,1)=6 and f(5,5)=11. You are given the value of m and (f(n,m)?n)⊕n,...
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu_2102_passed_sorce
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
hdu 1166线段树代码
自己做的HDU ACM已经AC的题目
HDU最全ac代码
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu动态规划算法集锦