hdu 3440 House Man(差分约束)

本文介绍了一道关于超人在不同高度的房子间跳跃的算法题目,目标是在限制条件下最大化起始和结束位置的距离。文章提供了完整的代码实现,涉及差分约束系统、SPFA算法等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2471    Accepted Submission(s): 1007


Problem Description
In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. Today he plans to use N houses to hone his house hopping skills. He will start at the shortest house and make N-1 jumps, with each jump taking him to a taller house than the one he is jumping from. When finished, he will have been on every house exactly once, traversing them in increasing order of height, and ending up on the tallest house. 
The man can travel for at most a certain horizontal distance D in a single jump. To make this as much fun as possible, the crazy man want to maximize the distance between the positions of the shortest house and the tallest house. 
The crazy super man have an ability—move houses. So he is going to move the houses subject to the following constraints:
1. All houses are to be moved along a one-dimensional path. 
2. Houses must be moved at integer locations along the path, with no two houses at the same location. 
3. Houses must be arranged so their moved ordering from left to right is the same as their ordering in the input. They must NOT be sorted by height, or reordered in any way. They must be kept in their stated order. 
4. The super man can only jump so far, so every house must be moved close enough to the next taller house. Specifically, they must be no further than D apart on the ground (the difference in their heights doesn't matter). 
Given N houses, in a specified order, each with a distinct integer height, help the super man figure out the maximum possible distance they can put between the shortest house and the tallest house, and be able to use the houses for training. 
 

Input
In the first line there is an integer T, indicates the number of test cases.(T<=500)
Each test case begins with a line containing two integers N (1 ≤ N ≤ 1000) and D (1 ≤ D ≤1000000). The next line contains N integer, giving the heights of the N houses, in the order that they should be moved. Within a test case, all heights will be unique. 
 

Output
For each test case , output “Case %d: “first where d is the case number counted from one, then output a single integer representing the maximum distance between the shortest and tallest house, subject to the constraints above, or -1 if it is impossible to lay out the houses. Do not print any blank lines between answers.
 

Sample Input
3 4 4 20 30 10 40 5 6 20 34 54 10 15 4 2 10 20 16 13
 

Sample Output
Case 1: 3 Case 2: 3 Case 3: -1
 

Author
jyd
 

题意:
有N个在一条直线上的房子, 每个房子有着不同的高度, 一个超人可以将这些房子左右移动

但不能改变房子之间的相对位置.

现在超人要从最矮的房子跳到刚好比他高的房子上面, 且每次跳的房子都要比当前房子要高.

那么最后超人肯定会跳到最高的房子上面, 现在给出超人能够跳的最远距离, 问: 如何摆放

这些房子, 使得超人能够经过所有的房子跳到最高的房子, 又要使最矮的房子和最高的房子

之间的距离最远??

思路:此题其实不难,不过跟一般的差分约束的区别在于一般的差分约束都是排队模型,也就是从一段到另一端的关系
而此题有可能来回跳,中间会出现一个|a[i+1].id-a[i].id|<=D
此时出现了绝对值怎么办呢?

注意差分约束的起点和终点必须按照从小到大的顺序,因为边都是有向边

我们约定有向边都是从编号小的到编号大的,然后我们最后起点和终点也换成从编号小的找编号大的即可

注意INF要大于1000000000

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1050
#define INF 1000000009
struct Edge
{
    int v,next,w;
} edge[N*N];
struct Node
{
    int id,v;
} p[N];
int cnt,head[N],n;
int d[N],vis[N],num[N];
void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
int spfa(int s,int t)
{
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    for(int i=1; i<=n; i++)
        d[i]=i==s?0:INF;
    vis[s]=1;
    queue<int>que;
    que.push(s);
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=0;
        num[u]++;
        if(num[u]>n) return -1;
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v,w=edge[i].w;
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                if(!vis[v])
                {
                    vis[v]=1;
                    que.push(v);
                }
            }
        }
    }
    return d[t];
}
bool cmp(Node a,Node b)
{
    return a.v<b.v;
}
int main()
{
    int T,q=1;
    int s,t,w,dis;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d %d",&n,&dis);
        int s,t,maxn=-INF,minn=INF;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&p[i].v);
            p[i].id=i;
            if(p[i].v<minn)
            {
                minn=p[i].v;
                s=i;
            }
            if(p[i].v>maxn)
            {
                maxn=p[i].v;
                t=i;
            }
        }
        for(int i=2; i<=n; i++)
            addedge(i,i-1,-1);
        sort(p+1,p+1+n,cmp);
        for(int i=2; i<=n; i++)
        {
            if(p[i].id>p[i-1].id)
                addedge(p[i-1].id,p[i].id,dis);
            else
                addedge(p[i].id,p[i-1].id,dis);
        }
        if(s>t) swap(s,t);
        printf("Case %d: ",q++);
        printf("%d\n",spfa(s,t));
    }
    return 0;
}


标题基于SpringBoot+Vue的社区便民服务平台研究AI更换标题第1章引言介绍社区便民服务平台的研究背景、意义,以及基于SpringBoot+Vue技术的研究现状和创新点。1.1研究背景与意义分析社区便民服务的重要性,以及SpringBoot+Vue技术在平台建设中的优势。1.2国内外研究现状概述国内外在社区便民服务平台方面的发展现状。1.3研究方法与创新点阐述本文采用的研究方法和在SpringBoot+Vue技术应用上的创新之处。第2章相关理论介绍SpringBoot和Vue的相关理论基础,以及它们在社区便民服务平台中的应用。2.1SpringBoot技术概述解释SpringBoot的基本概念、特点及其在便民服务平台中的应用价值。2.2Vue技术概述阐述Vue的核心思想、技术特性及其在前端界面开发中的优势。2.3SpringBoot与Vue的整合应用探讨SpringBoot与Vue如何有效整合,以提升社区便民服务平台的性能。第3章平台需求分析与设计分析社区便民服务平台的需求,并基于SpringBoot+Vue技术进行平台设计。3.1需求分析明确平台需满足的功能需求和性能需求。3.2架构设计设计平台的整体架构,包括前后端分离、模块化设计等思想。3.3数据库设计根据平台需求设计合理的数据库结构,包括数据表、字段等。第4章平台实现与关键技术详细阐述基于SpringBoot+Vue的社区便民服务平台的实现过程及关键技术。4.1后端服务实现使用SpringBoot实现后端服务,包括用户管理、服务管理等核心功能。4.2前端界面实现采用Vue技术实现前端界面,提供友好的用户交互体验。4.3前后端交互技术探讨前后端数据交互的方式,如RESTful API、WebSocket等。第5章平台测试与优化对实现的社区便民服务平台进行全面测试,并针对问题进行优化。5.1测试环境与工具介绍测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值