hdu 3440(差分约束好题)
House Man
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2410 Accepted Submission(s): 978
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.
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.
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.
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13
Case 2: 3
Case 3: -1
#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(差分约束好题)的更多相关文章
- hdu 3440 差分约束
看完题目第一遍,感觉很简单.当写完程序跑测试用例的时候,发现第二个总是过不了,然后好好研究了一下测试用例,才知道原来不是程序有问题,而是我的建图方式错了.对于这些无序的点,如果高的在右边,不等式是di ...
- hdu 1531(差分约束)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1531 差分约束的题之前也碰到过,刚好最近正在进行图论专题的训练,就拿来做一做. ①:对于差分不等式,a ...
- hdu 3666(差分约束,手动栈解决超时问题)
THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- I - 动物狂想曲 HDU - 6252(差分约束)
I - 动物狂想曲 HDU - 6252 雷格西桑和路易桑是好朋友,在同一家公司工作.他们总是一起乘地铁去上班.他们的路线上有N个地铁站,编号从1到N.1站是他们的家,N站是公司. 有一天,雷格西桑起 ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- poj 3159(差分约束经典题)
题目链接:http://poj.org/problem?id=3159思路:题目意思很简单,都与给定的条件dist[b]-dist[a]<=c,求dist[n]-dist[1]的最大值,显然这是 ...
- hdu 1364(差分约束)
King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12056 Accepted: 4397 Description ...
- hdu 4598 差分约束
思路:首先就是判断是否有奇环,若存在奇环,则输出No. 然后用差分约束找是否符合条件. 对于e(i,j)属于E,并且假设顶点v[i]为正数,那么v[i]-v[j]>=T--->v[j]-v ...
- poj 1201 Intervals——差分约束裸题
题目:http://poj.org/problem?id=1201 差分约束裸套路:前缀和 本题可以不把源点向每个点连一条0的边,可以直接把0点作为源点.这样会快许多! 可能是因为 i-1 向 i 都 ...
随机推荐
- Python知识点入门笔记——特色数据类型(列表)
Python中提供了列表这种数据类型(类型为list)来存储多个值构成的序列 用逗号将不同数据分隔开,整体放在一个方括号[]里就创建了列表 列表中的数据类型可以是相同的,也可以是不同的 列表中还可以嵌 ...
- Java总结 - List实现类ArrayList&LinkedList
本文是根据源码进行学习的,如果我有什么理解不对的地方请多指正,谢谢您 上面基本就是List集合类的类图关系了,图中省略掉了比如Cloneable等标记接口,那么List分别具体的主要实现类有:Arra ...
- Nodejs-内置核心模块&npm包管理工具
1.核心模块的意义 如果只是在服务器运行JavaScript代码,其实意义不大(浏览器就可以解决)因为无法实现功能(读写文件,访问网络) Node的用处在于本身还提供了一系列的功能模块,用于与操作系统 ...
- 设计模式之第12章-享元模式(Java实现)
设计模式之第12章-享元模式(Java实现) “怎么回事,竟然出现了OutOfMemory的错误.鱼哥,来帮我看看啊.”“有跟踪错误原因么?是内存泄露么?”“不是内存泄露啊,具体原因不知道啊.对了,有 ...
- bugku 普通的二维码
记录下对进制转换实现的重新思考. 扫描二维码扫到了一句垃圾话. 拖到winhex里面. 一开始以为是十进制直接转ascii,发现错误. 后来发现,最大数是7,八进制转换吧. 我是打算用python的l ...
- sklearn快速入门
原创博文,转载请注明出处. (为了节约空间,打印结果常用"..."表示省略) 一.加载数据集 1. 加载sklearn自带的数据集 scikit-learn有一些自带的标准数据集, ...
- 浅谈JavaScript中的函数问题
前面的话:JavaScript可运行在所有主要平台的主流浏览器上,也可运行在每一个主流操作系统的服务器端上.所以呢,要想成为一名优秀的全栈工程师,必须懂得JavaScript语言.这是我整理的JS的部 ...
- VS2015 +.NETMVC5 +EF实践
-- 当做笔记,以上图片按照顺序来的. 跟着 http://www.cnblogs.com/sanshi/ 一步步来的
- LAMP第一部分安装mysql -apache -php
1. 安装mysqlcd /usr/local/src/ 免安装编译二进制的包wget http://syslab.comsenz.com/downloads/linux/mysql-5.1.40-l ...
- 做一个APP
前言 有点零乱,但是我想写下来慢慢整理,搭建一个好点的工程-模式MVC, 会包括一些第三方库,动画库,第三方库管理关联,自定义常用控件的管理和关联 1.预编译文件的创建 在build setting ...