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 都 ... 
随机推荐
- day13内置函数
			内置函数 一.三元表达式 def max2(x,y): if x>y: return x else: return y res=max2(10,11) print(res) 三元表达式仅应用于: ... 
- 传送流(TS)的基础知识
			数字电视的TS包和TS流的组成和功能 综合考虑几下几个因素: (1)包的长度不能过短,否则包头开销所占比例过大, 导致传输效率下降 (2)包的长度不能过长,否则在丢失同步的情况下恢复同步的 周期过长, ... 
- 菜鸟学Linux - bash的配置文件
			bash是各大Linux发行版都支持的shell.当我们登陆bash的时候,虽然我们什么都没做,但是我们已经可以在bash中调用各种各样的环境变量了.这是因为,系统中已经定义了一系列的配置文件,以及加 ... 
- Java并发——synchronized和ReentrantLock的联系与区别
			0 前言 本文通过使用synchronized以及Lock分别完成"生产消费场景",再引出两种锁机制的关系和区别,以及一些关于锁的知识点. 本文原创,转载请注明出处:http:// ... 
- Python虚拟机类机制之绑定方法和非绑定方法(七)
			Bound Method和Unbound Method 在Python中,当对作为属性的函数进行引用时,会有两种形式,一种称为Bound Method,这种形式是通过类的实例对象进行属性引用,而另一种 ... 
- Python框架之Django学习笔记(十四)
			Django站点管理(续·完) 本想昨天更新的,谁曾想昨天竟然是工作日!我就不吐槽昨天加班到十一点多了,需求增加无疑让我等蛋疼不已,忽而想起一首打油诗: 明月几时有,把酒问群友.不知这次版本,今晚能出 ... 
- 解决pymysql不能实时查询最新的数据
			#在网上查询到的原因为: InnoDB 的默认隔离级别.它可以防止任何被查询的行被其他事务更改,从而阻止不可重复的读取,而不是 幻读取.它使用中度严格的锁定策略,以便事务内的所有查询都会查看同一快照中 ... 
- Python学习-day14-HTML
			以下博客为转载 http://www.cnblogs.com/evilliu/p/5750539.html 一:HTML(HyperText Markup Language)介绍 超文本标记语言,标准 ... 
- pythonic编程示例及简析
			1.列表 list[起始:结尾:增量] 值传递与地址传递 a = [2,1] b = a #地址传递 b = a[:] 值传递 a.sort() print a #[1,2] print b #[1, ... 
- 关于C++ STL标准库中map 的多元素应用
			map的特性是,所有的元素会根据键值自动排序.map的所有元素都是pair,同时拥有实值(value)和键值(key).pair的第一个元素被视为键值,第二个被视为实质piar 的定义 templat ... 
