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

Jimmy想要在森林中尝试不同的路线并确保在天黑前回家。他考虑从A到B的路径是有进步的,如果存在一条从B到家的路线比从A到家的任何路线都短。问题要求计算可能的不同路线数量。输入输出描述了问题的格式。解决方案包括从家开始计算最短路径,并从办公室使用记忆化搜索的动态规划求解路径。
部署运行你感兴趣的模型镜像

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





您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

在题目UVA323 Jury Compromise中,不适合将 (d + p) 的值作为动态规划(dp)状态。 在这道题里,主要需要考虑的是挑选元素的个数、元素间的差值以及元素的和。从引用[3]的分析来看,初考虑一维 `f[i]` 表示前 `i` 个元素中挑的小差值,发现不能递推,增加第二维表示差值,即 `f[i][j]` 表示从 `i` 个元素中挑出差值 `j` 是否可能,之后考虑到挑选元素个数的限制,又增加一维表示元素个数,形成 `f[i][j][k]` 表示从前 `i` 个元素挑 `k` 个元素达到 `j` 差值的大和。 若将 (d + p) 的值作为dp状态,难以处理挑选元素个数的限制以及元素间差值的问题,无法清晰地描述状态转移,也就难以建立有效的递推关系。而以差值和挑选元素个数作为状态维度,能很好地契合题目的约束条件,方便进行状态转移和问题求解。 ```python # 以下为示例代码框架,用于说明类似思路的状态转移,但并非完整代码 # 假设输入的元素数量为n,需要挑选的元素数量为m n = 10 m = 5 # 这里设置差值的平移量,因为差值可能为负 offset = 400 # 初始化dp数组 dp = [[[-float('inf')] * (2 * offset) for _ in range(m + 1)] for _ in range(n + 1)] # 初始化边界条件 dp[0][0][offset] = 0 # 进行状态转移 for i in range(1, n + 1): for k in range(0, min(i, m) + 1): for j in range(2 * offset): # 不选第i个元素 dp[i][k][j] = dp[i - 1][k][j] if k > 0: # 选第i个元素,这里d和p是第i个元素的差值和和 d, p = 1, 2 # 这里假设第i个元素的d和p值 new_j = j - d + offset if 0 <= new_j < 2 * offset: dp[i][k][j] = max(dp[i][k][j], dp[i - 1][k - 1][new_j] + d + p) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值