题目来源:
http://acm.hdu.edu.cn/showproblem.php?pid=3440
题目描述:
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
Author
jyd
Source
2010 ACM-ICPC Multi-University Training Contest(1)——Host by FZU
解题思路:
题目大意就是给你一列高度,一个人从最低高度递增往别的楼跳,每次跳的距离不能超过k,求最高楼和最低楼的最大距离,我们很容易可以发现这是一题差分约束的题,先排序每栋楼,记下原来的编号,对相邻的两个点建边,比如样例,排序后是10,20,30,40,编号3,1,2,4,那么1-3<=4就是连边addedge(3,1,4),以此类推,还有就是原来相邻的两栋距离至少有一,也要连边,然后跑3到4的最短路就行了,有负环就说明无解。。。
代码:
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn=1e5+10;
struct newt
{
int to,next,cost;
}e[2010];
struct newtt
{
int num,sum;
}dian[1010];
int head[1010],cnt,vis[1010],n,d,cs[1010],T,S;
ll dis[1010];
void addedge(int u,int v,int w)
{
e[cnt].to=v;
e[cnt].cost=w;
e[cnt].next=head[u];
head[u]=cnt++;
}
bool cmp(newtt a,newtt b)
{
return a.sum<b.sum;
}
bool spfa()
{
for(int i=1;i<=n;i++)
dis[i]=1e12,vis[i]=0,cs[i]=0;
queue<int>q;
q.push(S);
dis[S]=0;
vis[S]=1;
cs[S]=1;
while(!q.empty())
{
int now=q.front();
q.pop();
vis[now]=0;
for(int i=head[now];i!=-1;i=e[i].next)
{
int v=e[i].to;
if(dis[v]>dis[now]+e[i].cost)
{
dis[v]=dis[now]+e[i].cost;
if(vis[v])continue;
vis[v]=1;
if(++cs[v]>n)return 0;
q.push(v);
}
}
}
return 1;
}
int main()
{
int t,flag=1;
scanf("%d",&t);
while(t--)
{
memset(head,-1,sizeof(head));
cnt=0;
printf("Case %d: ",flag++);
scanf("%d%d",&n,&d);
for(int i=1;i<=n;i++)scanf("%d",&dian[i].sum),dian[i].num=i;
sort(dian+1,dian+n+1,cmp);
S=max(dian[1].num,dian[n].num);T=min(dian[1].num,dian[n].num);
for(int i=1;i<n;i++)
{
int u=max(dian[i].num,dian[i+1].num),v=min(dian[i].num,dian[i+1].num);
addedge(u,v,d);
}
for(int i=1;i<n;i++)addedge(i,i+1,-1);
if(spfa())printf("%d\n",dis[T]);
else puts("-1");
}
return 0;
}

解决一个经典的路径规划问题,帮助一位不能飞行但能跳跃的超级英雄,在一系列高度不等的建筑间跳跃,确保每次跳跃都能到达更高的建筑,同时限制最大跳跃距离。
 题解&spm=1001.2101.3001.5002&articleId=82632829&d=1&t=3&u=c62184bebe174388b2f644dc4d0102c2)
236

被折叠的 条评论
为什么被折叠?



