House Man

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

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
题意:一个人要从高度最低的房子开始跳到高度最高的房子,每次只能跳到与它本身的高度相邻的房子,每次能够跳的最大距离是D,问最低的房子和最高的房子的最大距离.
题解:这题我首先就想排序,然后再按编号对两个房子连一条边..结果死活做不出来,然后发现漏掉了一点,我们是要考虑房子的编号的。。有向边我们规定是从编号小的连向编号大的,所以我们无论是建图还是找最短路,都要从编号小的找到编号大的。这一点比较难想到.然后就很简单了,如果有负环或者没有松弛就不可达。其余情况输出最小值。
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdlib.h>
#include <queue>
using namespace std;
const int N = ;
const int INF = <<;
struct Node{
int h,id;
}node[N];
struct Edge{
int v,w,next;
}edge[N*(N-)];
int head[N],n;
void addEdge(int u,int v,int w,int &k){
edge[k].v = v,edge[k].w=w,edge[k].next=head[u],head[u]=k++;
}
int cmp(Node a,Node b){
return a.h<b.h;
}
bool vis[N];
int low[N];
int time[N];
int spfa(int s,int e){
queue<int>q;
for(int i=;i<=n;i++){
vis[i] = false;
time[i] = ;
low[i] = INF;
}
time[s]++;
low[s] = ;
q.push(s);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = false;
for(int k = head[u];k!=-;k=edge[k].next){
int v = edge[k].v,w=edge[k].w;
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
if(time[v]++>n) return -;
}
}
}
}
if(low[e]>=INF) return -;
return low[e];
}
int main()
{
int tcase;
scanf("%d",&tcase);
int t=;
while(tcase--){ memset(head,-,sizeof(head));
int D;
int tot = ;
scanf("%d%d",&n,&D);
for(int i=;i<=n;i++){
scanf("%d",&node[i].h);
node[i].id = i;
}
sort(node+,node++n,cmp);
for(int i=;i<n;i++){
addEdge(i+,i,-,tot);
}
for(int i=;i<n;i++){
int a = max(node[i].id,node[i+].id);
int b = min(node[i].id,node[i+].id);
addEdge(b,a,D,tot);
}
int s = min(node[n].id,node[].id);
int e = max(node[n].id,node[].id);
int c = spfa (s,e);
printf ("Case %d: %d\n",t++,c);
}
}

hdu 3440(差分约束好题)的更多相关文章

  1. hdu 3440 差分约束

    看完题目第一遍,感觉很简单.当写完程序跑测试用例的时候,发现第二个总是过不了,然后好好研究了一下测试用例,才知道原来不是程序有问题,而是我的建图方式错了.对于这些无序的点,如果高的在右边,不等式是di ...

  2. hdu 1531(差分约束)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...

  3. hdu 3666(差分约束,手动栈解决超时问题)

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. I - 动物狂想曲 HDU - 6252(差分约束)

    I - 动物狂想曲 HDU - 6252 雷格西桑和路易桑是好朋友,在同一家公司工作.他们总是一起乘地铁去上班.他们的路线上有N个地铁站,编号从1到N.1站是他们的家,N站是公司. 有一天,雷格西桑起 ...

  5. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  6. poj 3159(差分约束经典题)

    题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...

  7. hdu 1364(差分约束)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12056   Accepted: 4397 Description ...

  8. hdu 4598 差分约束

    思路:首先就是判断是否有奇环,若存在奇环,则输出No. 然后用差分约束找是否符合条件. 对于e(i,j)属于E,并且假设顶点v[i]为正数,那么v[i]-v[j]>=T--->v[j]-v ...

  9. poj 1201 Intervals——差分约束裸题

    题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...

随机推荐

  1. 10.VUE学习之使用lodash库减少watch对后台请求的压力

    问题描述 使用watch监听库里word的值的变化,获取新值后,用oxios发送的ajax异步请求, 此时会多次发送请求,浪费服务器资料. 解决办法 使用lodash库里的_.debounce函数延缓 ...

  2. Android系统中标准Intent的使用

    Android系统用于Activity的标准Intent 1.根据联系人ID显示联系人信息= Intent intent=new Intent(); intent.setAction(Intent.A ...

  3. Python基础学习总结__Day1

    一.Python是一门什么类型语言 1.解释型:一边编译一边执行,劣势是运行速度慢,但通过运用PyPy交互解释器(JIT技术)会让python程序执行速度快很多.优势是可移植性强. 2.强类型:即类型 ...

  4. 学习Spring框架系列(一):通过Demo阐述IoC和DI的优势所在

    Spring框架最核心东西便是大名鼎鼎的IoC容器,主要通过DI技术实现.下面我通过Demo的演变过程,对比学习耦合性代码,以及解耦和的过程,并深入理解面向接口编程的真正内涵. 这个例子包括如下几个类 ...

  5. 学习pytho第l六天 常用字符串用法

    name='my name is dream' print(name.capitalize())#首字母大写 print(name.count(‘’a‘’))#判断字符串里有多少个a print(na ...

  6. 初学python来进行odoo12版本开发

    这是我的第一篇博客.请多多指教! 首先要下载odoo-12的源代码 官方下载路径:          https://github.com/odoo/odoo/archive/12.0.zip 随便新 ...

  7. JQ剪辑图片插件,适用于移动端和PC端

    主要用到以下JS文件: <script src="js/photo/iscroll-zoom.js"></script> <script src=&q ...

  8. OpenCV学习笔记(一) 环境配置

    Visual Studio 2010 VS2010对应的OpenCV的lib文件(build\x86\vc10\lib)分为debug模式和release模式两种:debug模式牺牲速度,但能提供更多 ...

  9. LOJ #6008. 「网络流 24 题」餐巾计划

    #6008. 「网络流 24 题」餐巾计划 题目描述 一个餐厅在相继的 n nn 天里,每天需用的餐巾数不尽相同.假设第 i ii 天需要 ri r_ir​i​​ 块餐巾.餐厅可以购买新的餐巾,每块餐 ...

  10. Struts1 部分源码学习

    Struts1工作原理    1.系统初始化(读取配置):初始化ModuleConfig对象       Struts框架是一个总控制器(ActionServlet)是一个Servlet,在web.x ...