uva 10917 Walk Through the Forest(最短路+DP路径,4级)
来源:互联网 发布:淘宝店主 编辑:程序博客网 时间:2024/05/22 11:13
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 61 3 21 4 23 4 31 5 124 2 345 2 247 81 3 11 4 13 7 17 4 17 5 16 7 15 2 16 2 10
Output for Sample Input
24
(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;}
- uva 10917 Walk Through the Forest(最短路+DP路径,4级)
- UVA 10917 Walk Through the Forest(最短路+dp)
- 【UVa】10917 A Walk Through the Forest 最短路+DP
- UVA - 10917 Walk Through the Forest (最短路+DP)
- UVA - 10917 Walk Through the Forest (最短路+DP)
- UVA 10917 - Walk Through the Forest(最短路`dijksta+DP)
- uva 10917 Walk Through the Forest(最短路)
- UVa 10917 Walk Through the Forest(最短路+DP)
- UVA 10917 - Walk Through the Forest(最短路优化DP)
- Uva 10917 - Walk Through the Forest 最短路
- uva 10917 - Walk Through the Forest(最短路+记忆化)
- uva 10917 - Walk Through the Forest(Dijkstra+Dp)
- A Walk Through the Forest 最短路+dp
- A Walk Through the Forest(最短路+dfs)
- A Walk Through the Forest(hdu1142,最短路+dfs)
- 杭电1142 A Walk Through the Forest (最短路+DFS+dp)
- HDU - 1142 A Walk Through the Forest (DP + 最短路)
- A Walk Through the Forest(最短路径+DFS)
- wubi安装ubuntu后,增加swap大小,优化swap的使用参数-----------让ubuntu健步如飞,为编译android源码准备
- 纯CSS实现箭头旋转
- 郁闷的RecipientEditTextView!!!
- 中国的高速公路和美国接轨了?
- 相位相关matlab实现
- uva 10917 Walk Through the Forest(最短路+DP路径,4级)
- A - Hunters题解
- 将txt文件中的数据批量插入到数据库中SqlBulkCopy
- ubuntu下卸载mysql
- 用win7远程桌面连接ubuntu!!
- 初窥Linux 之 数据流重定向
- WSAEventSelect模型客户端代码示例
- 跟Google学习Android开发-起始篇-保存数据
- 未来有多远