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

来源:互联网 发布:淘宝店主 编辑:程序博客网 时间:2024/05/22 11:13

Description

Download as PDF

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;}





原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 剪了层次的头发怎么办 小米6充电线坏了怎么办 小米6导航信号弱怎么办 麦多多充不了电怎么办 一加数据线坏了怎么办 小米耳机泡水了怎么办 公司拖欠工资公司破产了怎么办 苹果x外壳掉漆怎么办 手机壳按键很硬怎么办 棉质白衣服染色怎么办 白棉t恤混洗染色怎么办 包包被衣服染色了怎么办 白色衣服染了菜汁怎么办 一加3t屏幕刺眼怎么办 怀孕吃了好多杏怎么办 门破了个洞怎么办 钢圈轮毂刮花了怎么办 瓷砖用刀子划了怎么办 陶瓷洗手台裂了怎么办 洗车泵水管坏了怎么办 印胶浆里面渗入了发泡浆怎么办? 管子断在水管里怎么办 衣服上的织带缩水怎么办 真丝衣服拔缝了怎么办 顾客说衣服太花怎么办 铝和碱反应变黑怎么办 40度高温多肉怎么办 沾到医用蓝药水怎么办? 裤子弄上泡沫胶怎么办 苍蝇纸粘衣服上怎么办 苍蝇胶沾衣服上怎么办 灯带为什么不亮怎么办 苹果6比屏幕变黄怎么办 雷腾键盘锁了怎么办 自吸泵电机不转怎么办 孕38周胎儿偏小怎么办 被火烧黑的铁怎么办 锅被烟熏黑了怎么办 墙壁被烟熏黑了怎么办 壁纸被烟熏黑了怎么办 空调被烟熏黑了怎么办