Description
Problem C: A Walk Through the Forest
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 ≤ 1000000indicating 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
Output for Sample Input
2 4
(apologies to) Richard Krueger
思路:从house开始做最短路,从office 开始用记忆化搜索DP计算路径。
dp[u]=sum(dp[v])u->v有路径且d[u]>d[v];d[x]为x到house的最短路。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int mm=1000+9;
const int oo=1e9;
const int mn=2e6+9;
class Edge
{
public:int v,dis,next;
}e[mn];
int head[mm],edge,n,m;
class heap
{
public:int u,d;
bool operator <(const heap& x)const
{
return d>x.d;
}
};
void data()
{
memset(head,-1,sizeof(head));edge=0;
}
void add(int u,int v,int _dis)
{
e[edge].v=v;e[edge].dis=_dis;e[edge].next=head[u];head[u]=edge++;
}
int d[mm],dp[mm];bool vis[mm];
void dijstra(int*d,int S)
{
priority_queue<heap>Q;
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;++i)
d[i]=oo;
d[S]=0;
Q.push((heap){S,0});
heap z;int u,v;
while(!Q.empty())
{
z=Q.top();Q.pop();
u=z.u;if(vis[u])continue;
vis[u]=1;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(d[v]>d[u]+e[i].dis)
{
d[v]=d[u]+e[i].dis;Q.push((heap){v,d[v]});
}
}
}
}
int dfs(int u)
{ int v;
if(dp[u])return dp[u];
if(u==2){dp[u]=1;return 1;}
for(int i=head[u];~i;i=e[i].next)
{ v=e[i].v;
if(d[v]<d[u])
dp[u]+=dfs(v);
}
return dp[u];
}
int main()
{int a,b,c;
while(~scanf("%d",&n)&&n)
{
scanf("%d",&m);
data();
for(int i=0;i<m;++i)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);add(b,a,c);
}
dijstra(d,2);
memset(dp,0,sizeof(dp));
printf("%d\n",dfs(1));
}
return 0;
}
Jimmy想要在森林中尝试不同的路线并确保在天黑前回家。他考虑从A到B的路径是有进步的,如果存在一条从B到家的路线比从A到家的任何路线都短。问题要求计算可能的不同路线数量。输入输出描述了问题的格式。解决方案包括从家开始计算最短路径,并从办公室使用记忆化搜索的动态规划求解路径。
464

被折叠的 条评论
为什么被折叠?



