hdu 4571 Travel in time (Floyd+记忆化搜索)

Travel in time

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1853    Accepted Submission(s): 374

Problem Description

  Bob gets tired of playing games, leaves Alice, and travels to Changsha alone. Yuelu Mountain, Orange Island, Window of the World, the Provincial Museum etc...are scenic spots Bob wants to visit. However, his time is very limited, he can’t visit them all.

  Assuming that there are N scenic spots in Changsha, Bob defines a satisfaction value Si to each spot. If he visits this spot, his total satisfaction value will plus Si. Bob hopes that within the limited time T, he can start at spot S, visit some spots selectively,
and finally stop at spot E, so that the total satisfaction value can be as large as possible. It‘s obvious that visiting the spot will also cost some time, suppose that it takes Ci units of time to visit spot i ( 0 <= i < N ).

  Always remember, Bob can choose to pass by a spot without visiting it (including S and E), maybe he just want to walk shorter distance for saving time.

  Bob also has a special need which is that he will only visit the spot whose satisfaction value is strictly larger than that of which he visited last time. For example, if he has visited a spot whose satisfaction value is 50, he would only
visit spot whose satisfaction value is 51 or more then. The paths between the spots are bi-directional, of course.

Input

  The first line is an integer W, which is the number of testing cases, and the W sets of data are following.

  The first line of each test data contains five integers: N M T S E. N represents the number of spots, 1 < N < 100; M represents the number of paths, 0 < M < 1000; T represents the time limitation, 0 < T <= 300; S means the spot Bob starts from. E indicates
the end spot. (0 <= S, E < N)

  The second line of the test data contains N integers Ci ( 0 <= Ci <= T ), which means the cost of time if Bob visits the spot i.

  The third line also has N integers, which means the satisfaction value Si that can be obtained by visiting the spot i ( 0 <= Si < 100 ).

  The next M lines, each line contains three integers u v L, means there is a bi-directional path between spot u and v and it takes L units of time to walk from u to v or from v to u. (0 <= u, v < N, 0 <= L <= T)

Output

  Output case number in the first line (formatted as the sample output).

  The second line contains an integer, which is the greatest satisfaction value.

If Bob can’t reach spot E in T units of time, you should output just a “0” (without quotation marks).

Sample Input

1
4 4 22 0 3
1 1 1 1
5 7 9 12
0 1 10
1 3 10
0 2 10
2 3 10

Sample Output

Case #1:
21

思路:把问题分解,先求出任意两点之间的最短路,然后从起点开始搜索,记录三个量对应的状态值:当前位置、上一个景点的满意度、剩余时间。

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 110
const int inf=0x3fffffff;
int T,n,m,t,s,e;
int f[N][N][N*3]; //dp[i][j][k]表示在第i个城市,前一个城市的满意度为j,还剩下k的时间
int g[N][N];
int c[N],w[N];
void Floyd()
{
    int i,j,k;
    for(k=0;k<n;k++)
    {
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
            }
        }
    }
}
int dfs(int u,int val,int t)
{
    if(f[u][val][t]!=-1)
        return f[u][val][t];
    if(t<g[u][e])
        return -inf;
    int tmp=0,i;
    for(i=0;i<n;i++)
    {
        if(w[i]<=val)
            continue;
        tmp=max(tmp,dfs(i,w[i],t-c[i]-g[u][i])+w[i]);
    }
    return f[u][val][t]=tmp;
}
int main()
{
    int i,j,u,v,d,cnt=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d%d",&n,&m,&t,&s,&e);
        for(i=0;i<n;i++)
            scanf("%d",&c[i]);
        for(i=0;i<n;i++)
            scanf("%d",&w[i]);
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                g[i][j]=(i==j?0:inf);
            }
        }
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&d);
            g[u][v]=g[v][u]=min(g[u][v],d);
        }
        Floyd();
        memset(f,-1,sizeof(f));
        printf("Case #%d:\n%d\n",cnt++,dfs(s,0,t));
    }
    return 0;
}
时间: 2024-08-05 18:17:51

hdu 4571 Travel in time (Floyd+记忆化搜索)的相关文章

HDU 4960 Another OCD Patient(记忆化搜索)

HDU 4960 Another OCD Patient 题目链接 记忆化搜索,由于每个碎片值都是正数,所以每个前缀和后缀都是递增的,就可以利用twopointer去找到每个相等的位置,然后下一个区间相当于一个子问题,用记忆化搜索即可,复杂度接近O(n^2) 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f

HDU 1208 Pascal&#39;s Travels( 记忆化搜索)

题目大意:每一小格代表能向右或者向下走几步,问从左上走到右下总共有多少种走法. dp[i][j]存放该格子有多少总走法. #include <iostream> #include <cstring> using namespace std; int n; char a[40][40]; int s[40][40]; __int64 dp[40][40]; int X[]={1, 0}; int Y[]={0, 1}; __int64 dfs(int x, int y) { if(d

HDU 1331 Function Run Fun (基础记忆化搜索)

Function Run Fun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2602    Accepted Submission(s): 1263 Problem Description We all love recursion! Don't we? Consider a three-parameter recursive f

HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

题目地址:HDU 1428 先用BFS+优先队列求出所有点到机房的最短距离,然后用记忆化搜索去搜. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #incl

HDU - 5001 Walk(概率dp+记忆化搜索)

Walk I used to think I could be anything, but now I know that I couldn't do anything. So I started traveling. The nation looks like a connected bidirectional graph, and I am randomly walking on it. It means when I am at node i, I will travel to an ad

HDU 2089 不要62(数位dp&amp;记忆化搜索)

题目链接:[kuangbin带你飞]专题十五 数位DP C - 不要62 题意 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众. 不吉利的数字为所有含有4或62的号码.例如: 62315 73418 88914 都属于不吉利号码.但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列. 你的任务是,对于每次给

HDU - 6143 Killer Names(dp记忆化搜索+组合数)

题意:从m种字母中选取字母组成姓名,要求姓和名中不能有相同的字母,姓和名的长度都为n,问能组成几种不同的姓名. 分析: 1.从m种字母中选取i种组成姓,剩下m-i种组成名. 2.i种字母组成长度为n的姓-----可转换成用i种颜色给n个球染色,记忆化搜索 dfs(n,i)---用i种颜色给n个球染色的方案数 先给第1个小球涂色,有m种选择,假设涂色为color[1], 那么剩下n-1个小球: 如果继续使用color[1],则问题转化为用m种颜色给n-1个小球涂色: 如果不再使用color[1],

HDU 1331 Function Run Fun(记忆化搜索)

Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <= 0 or b <= 0 or c <= 0, then w(a, b, c) returns:1 if a > 20 or b > 20 or c > 20, then w(a, b, c) returns:w(20, 20, 20)

HDU 1078 FatMouse and Cheese 简单记忆化搜索

题意是:给你n和k,一个老鼠从左上角开始走,每次可以往一个方向走1~k中的任何一个值,但是每一步必须比前一步的值大,问获取的最多的值是多少? 简单记忆化搜索,dp[i][j]表示当前位置能获取的最大值,但是要注意,考虑全所有的情况才能用记忆化搜索,只要没有后效性,所有dfs,我觉得理论上都能用记忆化搜索. #include <cstdio>#include <iostream>#include <vector>#include <cmath>#include

HDU 4597 Play Game (DP,记忆化搜索,博弈)

题意:Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个,只能从其中一个序列,选择两端中的一个拿走.他们都希望可以拿到尽量大的数字之和, 并且他们都足够聪明,每次都选择最优策略.Alice先选择,问最终Alice拿到的数字总和是多少? 析:很明显的一个博弈题,但是用记忆化搜索来解决的,用d[la][ra][lb][rb]记录的是在a的区间只剩下la~ra,b的区间只剩下lb~rb的时候,Alice能得到的最大值, 那么我应该在让Bob取最大值中的最小才能满足这个题,当是A