hdoj--3440--House Man(差分约束)
House Man
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.
-1 if it is impossible to lay out the houses. Do not print any blank lines between answers.
3
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13
Case 1: 3
Case 2: 3
Case 3: -1
题意:一个男人在n个房子之间跳,他只可以从低的房子跳到高的房子上,
现在给出房子的个数n,男人跳的有多远,每一个房子的高度,男人不能
改变房子的位置,只能按照房子的输入顺序,问男人能否跳n-1次经过所有的
房子到达最高的房子上,如果可以的话输出最大的距离值,否则输出-1
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f
#define MAXN 1010
#define MAXM 20000
int head[MAXN],vis[MAXN],dis[MAXN],used[MAXN];
int Instack[MAXN];
int D,n,s,e,cnt;
struct node
{
int u,v;
int val,next;
}edge[MAXM];
struct house
{
int height,pos;
}h[MAXN];
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v,int val)
{
node E={u,v,val,head[u]};
edge[cnt]=E;
head[u]=cnt++;
}
int cmp(house s1,house s2)
{
return s1.height<s2.height;
}
void getmap()
{
for(int i=1;i<=n;i++)
cin>>h[i].height,h[i].pos=i;
sort(h+1,h+n+1,cmp);
s=min(h[1].pos,h[n].pos);
e=max(h[1].pos,h[n].pos);
for(int i=1;i<=n-1;i++)
{
if(h[i].pos>h[i+1].pos)
add(h[i+1].pos,h[i].pos,D);
else
add(h[i].pos,h[i+1].pos,D);
add(i+1,i,-1);
}
}
void SPFA()
{
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
memset(dis,INF,sizeof(dis));
memset(Instack,0,sizeof(Instack));
used[s]++;
vis[s]=1;
dis[s]=0;
int top=0;
Instack[top++]=s;
while(top)
{
int u=Instack[--top];
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
node E=edge[i];
if(dis[E.v]>dis[u]+E.val)
{
dis[E.v]=dis[E.u]+E.val;
if(!vis[E.v])
{
vis[E.v]=1;
used[E.v]++;
if(used[E.v]>n)
{
cout<<-1<<endl;
return ;
}
Instack[top++]=E.v;
}
}
}
}
cout<<dis[e]<<endl;
}
int main()
{
int t;
int k=1;
// std::cout.sync_with_stdio(false);
cin>>t;
while(t--)
{
cin>>n>>D;
init();
getmap();
cout<<"Case "<<k++<<": ";
SPFA();
}
return 0;
}
hdoj--3440--House Man(差分约束)的更多相关文章
- HDOJ 1534 Schedule Problem 差分约束
差分约数: 求满足不等式条件的尽量小的值---->求最长路---->a-b>=c----> b->a (c) Schedule Problem Time Limit: 2 ...
- HDOJ 1384 差分约束
结题报告合集请戳:http://972169909-qq-com.iteye.com/blog/1185527 /*题意:求符合题意的最小集合的元素个数 题目要求的是求的最短路, 则对于 不等式 f( ...
- 图论--差分约束--HDU\HDOJ 4109 Instrction Arrangement
Problem Description Ali has taken the Computer Organization and Architecture course this term. He le ...
- 【转】最短路&差分约束题集
转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...
- 转载 - 最短路&差分约束题集
出处:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★ ...
- Candies-POJ3159差分约束
Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- 2014 Super Training #6 B Launching the Spacecraft --差分约束
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
随机推荐
- BZOJ 4033 树形DP
http://blog.csdn.net/mirrorgray/article/details/51123741 安利队长blog- 树形dp吧,状态挺显然的,dp[x][j]表示以x为根的子树中,选 ...
- 状态模式(state)C++实现
状态模式 当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类. 状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况.把状态的判断逻辑转移到表示不同状态的一系列类 ...
- MVC:@RenderBody、@RenderPage、@RenderSection用法
本文导读:在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.模板页:@RenderBody()占位符:局部页面:@RenderP ...
- params可变参数、SqlCommand.Parameters.add()方法
namespace params可变参数{ class Program { static void Main(string[] args) { int[] num = {66,99,55,44, }; ...
- <script runat=server>与<%%>,<%=%>与<%response.write%>
我想问一下:在语句<script runat="server"> </script>中编写后台代码和在后台.cs文件中编写后台代码有什么不同,执行效率会不会 ...
- 位姿检索PoseRecognition:LSH算法.p稳定哈希
位姿检索使用了LSH方法,而不使用PNP方法,是有一定的来由的.主要的工作会转移到特征提取和检索的算法上面来,有得必有失.因此,放弃了解析的方法之后,又放弃了优化的方法,最后陷入了检索的汪洋大海. 0 ...
- APUE学习笔记3——文件和目录
1 简介 之前学习了执行I/O操作的基本函数,主要是围绕普通文件I/O的打开.读或写.下面继续学习Unix文件系统的其他特征和文件的基本性质.我们将从stat函数开始,了解stat结构所代表的文件属性 ...
- NEFU 116 两仪剑法 【求最小公倍数】
题目链接:http://acm.nefu.edu.cn/JudgeOnline/status.php?problem_id=116&order=1 解题思路:求最小公倍数 #include&l ...
- vc++如何创建程序-函数的重载
重载构成的条件:函数的参数类型,参数个数不同,才能构成函数的重载 函数重载分为两种情况: 1 .(1)void output(); (2)int output(); 2 .(1)void output ...
- 在虚拟机安装Oracle,在本地电脑上运行sql develer出现ora-12514
问题是解决了,先描述一下,本人用的虚拟机系统是server 2003 1.打开虚拟机,然后不管了 2.打开本地的sql developer,登录,出现了一个小白框:ora-12514:listener ...