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. 科学计算库Numpy——运算

    np.multiply(array1,array2) 该函数用于数组中对应位置上的数相乘. 一维向量 二维数组 np.dot(array1,array2) 两个数组都是一维向量 数组中对应位置上的数相 ...

  2. Nosql和RDBMS的比较及解释

    概述 传统的关系型数据库以及数据仓库在面对大数据的处理时显得越来越力不从心.因为关系数据库管理系统 (RDBMS)的设计从未考虑过能够处理日益增长且格式多变的数据,以及访问数据并进行分析的用户需求呈爆 ...

  3. Hive UDTF开发指南

    在这篇文章中,我们将深入了解用户定义表函数(UDTF),该函数的实现是通过继承org.apache.Hadoop.hive.ql.udf.generic.GenericUDTF这个抽象通用类,UDTF ...

  4. Diycode开源项目 TopicContentActivity分析

    1.效果预览以及布局分析 1.1.实际效果预览 左侧话题列表的布局是通过TopicProvider来实现的,所以当初分析话题列表就没有看到布局. 这里的话题内容不是一个ListView,故要自己布局. ...

  5. Spring加载配置文件的几种方法(org.springframework.beans.factory.BeanDefinitionStoreException)

    一:Spring中的几种容器都支持使用xml装配bean,包括:XmlBeanFactory ,ClassPathXmlApplicationContext ,FileSystemXmlApplica ...

  6. 【ELK】ELK安装与配置

    一.ELK体系结构 二.系统环境变量 [主机信息] IP 主机名 操作系统版本 10.10.10.102 console CentOS7.5 10.10.10.103 log1 CentOS7.510 ...

  7. pycharm中某些方法被标黄的原因及解决办法

    在编辑python文件时,会遇到上图所示,函数方法被标黄的问题,但是不影响使用. 引起原因:,如果不报错说明,这是因为你配置的python解释器中有该方法,但是pycharm没有找到这个方法,即加载失 ...

  8. 基础_String

    String str1="hello"; String str2="hello"; String str3="hello"; String ...

  9. loj2045 「CQOI2016」密钥破解

    CQOI 板子大赛之 pollard rho #include <iostream> #include <cstdio> using namespace std; typede ...

  10. 微信公众开发api接口

      简介 微信公众平台消息接口为开发者提供了一种新的消息处理方式.微信公众平台消息接口为开发者提供与用户进行消息交互的能力.对于成功接入消息接口的微信公众账号,当用户发消息给公众号,微信公众平台服务器 ...