您的位置:首页 > 其它

uva 10917 Walk Through the Forest(最短路+DP路径,4级)

2013-07-07 12:04 543 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: