hdu4751 最短路+背包dp

http://acm.hdu.edu.cn/showproblem.php?pid=4571

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
/**
hdu4751  最短路+背包dp
题目大意:给定一个图网络,想要从起点走到终点,可以访问一些点,访问需要耗费一定的时间,每访问一个点都可以得到该点的一个满意值,当然也可以选择路过该点,路过不
          需要时间,那么问题来了:在给定时间内能否从起点走到终点,如果能可以得到的最大花费值是多少?
解题思路:这个题目的意思有很多细节需要揣摩:1,路过点不用花费时间,但也不能得到满意值,2,最后到达终点即可,不一定非要访问终点
          先求出每两个点之间的最短路,然后dp转移。dp的时候注意一点,访问的下一点要比当前点的满意度要高,这就要求我们在算满意度点为i的点的时候,所有满意度
          小于i的点的状态都要已知,所以处理的时候要满意度递增先排个序。。最后值得一提的是一定要注意重边,坑死我了
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=105;
int n,m,s,t,e;
int a[maxn][maxn],dp[maxn][350],Hash[maxn];
struct note
{
    int w,v,id;
    bool operator <(const note &other) const
    {
        return v<other.v;
    }
} p[maxn];
int main()
{
    int T,tt=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d%d",&n,&m,&t,&s,&e);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&p[i].w);
            p[i].id=i;
        }
        for(int i=0; i<n; i++)
        {
            scanf("%d",&p[i].v);
        }
        sort(p,p+n);
        for(int i=0; i<n; i++)
        {
            Hash[p[i].id]=i;
        }
        for(int i=0; i<=100; i++)
        {
            for(int j=0; j<=100; j++)
            {
                if(i==j)
                    a[i][j]=0;
                else
                    a[i][j]=INF;
            }
        }
        while(m--)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            u=Hash[u],v=Hash[v];
            a[u][v]=min(a[u][v],w);///重边取最小,不写必错,我都快哭了==shit
            a[v][u]=min(a[v][u],w);
        }
        for(int k=0; k<n; k++)
        {
            for(int i=0; i<n; i++)
            {
                if(a[i][k]!=INF)
                {
                    for(int j=0; j<n; j++)
                    {
                        if(a[k][j]!=INF)
                        {
                            a[i][j]=min(a[i][j],a[i][k]+a[k][j]);
                        }
                    }
                }
            }
        }
        s=Hash[s];
        e=Hash[e];
        memset(dp,-1,sizeof(dp));
        for(int i=0; i<n; i++)
        {
            for(int j=p[i].w+a[i][s]; j<=t; j++)
            {
                dp[i][j]=p[i].v;
            }
        }
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<i; j++)
            {
                if(p[i].v==p[j].v)break;
                for(int k=0; k<=t; k++)
                {
                    if(k<a[i][j]+p[i].w)continue;
                    if(dp[j][k-a[i][j]-p[i].w]==-1)continue;
                    dp[i][k]=max(dp[i][k],dp[j][k-a[i][j]-p[i].w]+p[i].v);
                }
            }
        }
        int maxx=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<=t; j++)
            {
                if(j+a[i][e]>t)break;
                maxx=max(maxx, dp[i][j]);
            }
        }
        printf("Case #%d:\n%d\n",++tt,maxx);
    }
    return 0;
}
/**
1
4 4 22 0 3
22 22 22 22
5 7 9 12
0 1 10
1 3 10
0 2 10
2 3 10
*/
				
时间: 2024-08-01 08:12:37

hdu4751 最短路+背包dp的相关文章

[BZOJ 1025] 游戏 置换群 背包DP

题意 对于一个 $n$ 阶置换群 $A$ , 它的循环节大小分别为 $a_1, a_2, ..., a_m$ , 则有 $\sum_{i = 1} ^ m a_i = n$ . 定义 $f(A)$ 为它的所有循环节的最小公倍数, 即 $f(A) = [a_1, a_2, ..., a_m]$ . 求在所有 $n$ 阶置换群中, $f(A)$ 有多少种取值. $n \le 1000$ . 分析 判断 $K$ 可不可取. $K = \prod_{i = 1} ^ r {s_r} ^ {t_r}$ 可

hdu 5234 Happy birthday 背包 dp

Happy birthday Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5234 Description 今天是Gorwin的生日.所以她的妈妈要实现她的一个愿望.Gorwin说她想吃很多蛋糕.所以他妈妈带她来到了蛋糕园. 这个园子被分成了n*m个方格子.在每一个格子里面,有一个蛋糕.第i行,第j列的格子中有一个重量为wij千克的蛋糕,Gorwin从左上角(1,1

hdu 1171 Big Event in HDU(背包DP)

题意: 杭电搬迁,有N种设备,每种设备有个价值V,数量M,要求将这些设备平分,使得平分后两边的总价值尽可能地相等. 输出两边各自的总价值. 思路: 背包DP后,P=所有的总价值/2,然后从P开始往两边找到第一个满足的价值. 可以降维,但是要注意for循环的顺序. 看代码. 代码: int v[55], m[55]; bool dp[250005]; int main(){ int n; while(scanf("%d",&n)!=EOF && n>=0){

POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了. 本题要求装满的,故此增加个限制就可以了. #include <stdio.h> #include <stdlib.h> #include <string.h> inline int min(int a, int b) { return a < b? a : b; } c

BZOJ 1042 硬币购物(完全背包+DP)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1042 题意:给出四种面值的硬币c1,c2,c3,c4.n个询问.每次询问用d1.d2.d3.d4个相应的硬币能够拼出多少种总和为s? 思路:(1)首先,用完全背包求出f[i]表示四种硬币的数量无限制拼出i的方案数. (2)接着我们来理解 x=f[s]-f[s-(d1+1)*c1]的含义:x表示c1硬币的数量不超过d1个而其他三种硬币的数量不限制拼成s的方案数.我们举着例子来说明, 假设

HDU 5616 Jam&#39;s balance 背包DP

Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell whether things on different side are the same weight.Weights can be put on left side or right side arbitrarily.Please tell whether the balance can me

hdu1561:树形背包dp

给定n个地点,每个地点藏有cost[i]的宝物,取得某些宝物有时需要先取其他宝物,现在让我们选m个地点问最多可以选多少宝物? 还是挺裸的树形背包dp吧,不难,关键还是中间dp的部分.可以做模板了->_-> 注意点:多组数据的话如果第一组对了然后其他都错了,那么很有可能是初始化的时候漏了.这次找可很久才知道差了e[0].clear().平时的习惯都是从1开始. --------------------------------------------------------------------

背包DP HDOJ 5410 CRB and His Birthday

题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == 1时,得到a[i] + b[i],若再买得到的是a[i],那么x == 1的情况用01背包思想,x > 1时就是在01的基础上的完全背包.背包dp没刷过专题,这么简单的题也做不出来:( /************************************************* Author

codeforces 148E Aragorn&#39;s Story 背包DP

Aragorn's Story Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/148/E Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who