Travel HDU - 4284(dfs或状压dp)

本文介绍了一个旅行规划问题,PP需要在有限的资金下访问特定城市并获取工作许可。文章提供了两种算法实现方案,一种是基于深度优先搜索(DFS)的方法,另一种是状态压缩动态规划(DP)方法。

PP loves travel. Her dream is to travel around country A which consists of N cities and M roads connecting them. PP has measured the money each road costs. But she still has one more problem: she doesn’t have enough money. So she must work during her travel. She has chosen some cities that she must visit and stay to work. In City_i she can do some work to earn Ci money, but before that she has to pay Di money to get the work license. She can’t work in that city if she doesn’t get the license but she can go through the city without license. In each chosen city, PP can only earn money and get license once. In other cities, she will not earn or pay money so that you can consider Ci=Di=0. Please help her make a plan to visit all chosen cities and get license in all of them under all rules above.
  PP lives in city 1, and she will start her journey from city 1. and end her journey at city 1 too.
Input
  The first line of input consists of one integer T which means T cases will follow.
  Then follows T cases, each of which begins with three integers: the number of cities N (N <= 100) , number of roads M (M <= 5000) and her initiative money Money (Money <= 10^5) .
  Then follows M lines. Each contains three integers u, v, w, which means there is a road between city u and city v and the cost is w. u and v are between 1 and N (inclusive), w <= 10^5.
  Then follows a integer H (H <= 15) , which is the number of chosen cities.
  Then follows H lines. Each contains three integers Num, Ci, Di, which means the i_th chosen city number and Ci, Di described above.(Ci, Di <= 10^5)
Output
  If PP can visit all chosen cities and get all licenses, output “YES”, otherwise output “NO”.
Sample Input
2
4 5 10
1 2 1
2 3 2
1 3 2
1 4 1
3 4 2
3
1 8 5
2 5 2
3 10 1
2 1 100
1 2 10000
1
2 100000 1
Sample Output
YES
NO

这题没注意有重边,当时看15想过dfs一发,而且点也特变少,不过犹豫了很久,不过dp是真的想到了,但是我想着是递归dp的(和商人旅行的模型是差不多的),但是超内存了,还是在主函数里转移方程。太久没有写过状压dp了,不过dfs的编码复杂度真的简单,噗~

dfs写法:

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 1000000000
using namespace std;
int dis[105][105];
void floyd(int n)
{
      for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                dis[i][j] =min(dis[i][j], dis[i][k] + dis[k][j]);
            }
}
int h;
int mapp[18];
int C[18];
int D[18];
bool vis[18];
bool sign;
 void dfs(int cur,int left,int lastP)
 {
     if(cur==h+1)
     {
         if(left>=dis[lastP][1])
            sign=true;
            return ;
     }
     for(int i=1;i<=h;i++)
     {
         if(vis[i])
            continue;
        if(left-dis[lastP][mapp[i]]-D[i]>=0&&!sign)
        {
            vis[i]=true;
            dfs(cur+1,left-dis[lastP][mapp[i]]-D[i]+C[i],mapp[i]);
            vis[i]=false;
        }
     }
 }
int main()
{
    int t;
    int n,m,money;
    scanf("%d",&t);
    int x,y,w;
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&money);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            dis[i][j]=INF;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&w);
            dis[x][y]=dis[y][x]=min(w,dis[y][x]);
        }
        for(int i=1;i<=n;i++)
            dis[i][i]=0;
        floyd(n);
        scanf("%d",&h);
        for(int i=1;i<=h;i++)
            scanf("%d%d%d",mapp+i,C+i,D+i);
        sign=false;
        memset(vis,false,sizeof(vis));
        dfs(1,money,1);
        if(sign)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

dp写法:

#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 1000000000
using namespace std;
int dis[105][105];
void floyd(int n)
{
      for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                dis[i][j] =min(dis[i][j], dis[i][k] + dis[k][j]);
            }
}
int dp[(1<<15)+5][16];
int mapp[18];
int C[18];
int D[18];
int m;
int main()
{
    int t;
    int n,money;
    scanf("%d",&t);
    int x,y,w;
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&money);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            dis[i][j]=INF;
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&w);
            dis[x][y]=dis[y][x]=min(w,dis[y][x]);
        }
        for(int i=1;i<=n;i++)
            dis[i][i]=0;
        floyd(n);
        int h;
        scanf("%d",&h);
        for(int i=1;i<=h;i++)
            scanf("%d%d%d",mapp+i,C+i,D+i);
        memset(dp,-1,sizeof(dp));
        for(int i=1;i<=h;i++)
        {
            int temp=money-dis[1][mapp[i]]-D[i];
            if(temp>=0)
                dp[1<<(i-1)][i]=temp+C[i];
        }
        int s=(1<<h)-1;
        for(int i=1;i<=s;i++)
        {
            for(int j=1;j<=h;j++)
            {
                if(dp[i][j]==-1)
                    continue;
                for(int k=1;k<=h;k++)
                {
                    if(i&(1<<(k-1)))
                        continue;
                    int temp=dp[i][j]-dis[mapp[j]][mapp[k]]-D[k];
                    if(temp>=0)
                    {
                        temp+=C[k];
                        int s0=i^(1<<(k-1));
                        dp[s0][k]=max(dp[s0][k],temp);
                    }
                }
            }
        }
        bool sign=false;
        for(int i=1;i<=h;i++)
        {
            //cout<<dp[s][i]<<endl;
            if(dp[s][i]>=dis[1][mapp[i]])
            {
                sign=true;
                break;
            }
        }
        if(sign)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
HDU-3480 是一个典型的动态规划问题,其题目标题通常为 *Division*,主要涉及二维费用背包问题优化后的动态规划策略。题目大意是:给定一个整数数组,将其划分为若干个连续的子集,每个子集最多包含 $ m $ 个元素,并且每个子集的最大值与最小值之差不能超过给定的阈值 $ t $,目标是使所有子集的划分代价总和最小。每个子集的代价是该子集最大值与最小值的差值。 ### 动态规划思路 设 $ dp[i] $ 表示前 $ i $ 个元素的最小代价。态转移方程如下: $$ dp[i] = \min_{j=0}^{i-1} \left( dp[j] + cost(j+1, i) \right) $$ 其中 $ cost(j+1, i) $ 表示从第 $ j+1 $ 到第 $ i $ 个元素构成一个子集的代价,即 $ \max(a[j+1..i]) - \min(a[j+1..i]) $。 为了高效计算 $ cost(j+1, i) $,可以使用滑动窗口单调队列等数据结构来维护区间最大值与最小值,从而将时间复杂度优化到可接受的范围。 ### 示例代码 以下是一个简化版本的动态规划实现,使用暴力方式计算区间代价,适用于理解问题结构: ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 10010; int a[MAXN]; int dp[MAXN]; int main() { int T, n, m; cin >> T; for (int Case = 1; Case <= T; ++Case) { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; dp[0] = 0; for (int i = 1; i <= n; ++i) { dp[i] = INF; int mn = a[i], mx = a[i]; for (int j = i; j >= max(1, i - m + 1); --j) { mn = min(mn, a[j]); mx = max(mx, a[j]); if (mx - mn <= T) { dp[i] = min(dp[i], dp[j - 1] + mx - mn); } } } cout << "Case " << Case << ": " << dp[n] << endl; } return 0; } ``` ### 优化策略 - **单调队列**:可以使用两个单调队列分别维护当前窗口的最大值与最小值,从而将区间代价计算的时间复杂度从 $ O(n^2) $ 降低到 $ O(n) $。 - **斜率优化**:若问题满足特定的决策单调性,可以考虑使用斜率优化技巧进一步加速态转移过程。 ### 时间复杂度分析 原始暴力解法的时间复杂度为 $ O(n^2) $,在 $ n \leq 10^4 $ 的情况下可能勉强通过。通过单调队列优化后,可以稳定运行于 $ O(n) $ $ O(n \log n) $。 ### 应用场景 HDU-3480 的问题模型可以应用于资源调度、任务划分等场景,尤其适用于需要控制子集内部差异的问题,如图像分块缩、数据分段处理等[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值