hdu 3440 (差分约束 最短路径)

House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3736    Accepted Submission(s): 1567


 

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

 

题意:要从高度最低的地方跳到高度最高的地方 每次跳跃的距离小于等于m 每次只能跳到下一个比他高但是和他高度最接近的位置 
每个位置都有一个高度值 并且顺序不能换 最低和最高的最远距离。

思路:差分约束系统

1.两个相邻点之间最小距离为一,则有d[i+1]-d[i]>=1,等价于  d[i]-d[i+1]<=-1;

2.高度最接近的两个点的最大距离为m,按距离排序之后可有     d[j+1]-d[j]<=m;

查分约束系统的条件找出来了;


剩下就是构图的事了。
由的d[i]-d[i+1]<=-1 的编辑条件可知,我们给各个点制定了一个方向,就是从左到右是依次增大的。
这就限定了的d[j+1]-d[j]<=m的连边条件,必须是由对应的序号小的点连向序号大的点

 

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
const int inf=0x3f3f3f3f;
struct node
{
	int to,dis,next;
}edge[N<<2];
int cnt;
int head[N];
void addedge(int from,int to,int dis)
{
	edge[cnt].to=to;
	edge[cnt].dis=dis;
	edge[cnt].next=head[from];
	head[from]=cnt++;
}
void In()
{
	cnt=0;
	memset(head,-1,sizeof(head));
}
struct node1
{
	int data,id;
	friend bool operator <(node1 A,node1 B){
		return A.data<B.data;
	}
}s[N];
int dis[N];
bool vis[N];
int num[N];
int spfa(int s1,int s2,int n)
{
	memset(num,0,sizeof(num));
	memset(dis,inf,sizeof(dis));
	memset(vis,false,sizeof(vis));
	queue<int>Q;
	while(!Q.empty()) Q.pop();
 	Q.push(s1);
	dis[s1]=0;
	vis[s1]=true;
	while(!Q.empty()){
		int u=Q.front();
		Q.pop();
		vis[u]=false;
		for(int i=head[u];i!=-1;i=edge[i].next){
			int to=edge[i].to;
			if(dis[to]>dis[u]+edge[i].dis){
				dis[to]=dis[u]+edge[i].dis;
				if(!vis[to]){
					vis[to]=true;
					Q.push(to);
					num[to]++;
					if(num[to]>n) return -1;
				}
			}
		}
		
	}
	return dis[s2];
}
int main()
{
	int T;
	scanf("%d",&T);
	int sw=1;
	while(T--){
		In();
		int n,m;
		scanf("%d %d",&n,&m);
		for(int i=0;i<n;i++){
			int x;
			scanf("%d",&x);
			s[i].data=x;
			s[i].id=i;
		}
		sort(s,s+n);
		bool flag=false;
		for(int i=0;i<n-1;i++){
			addedge(i+1,i,-1);
			int u=min(s[i].id,s[i+1].id);
			int v=max(s[i].id,s[i+1].id);
			if(v-u>m) flag=true;
			addedge(u,v,m);
		}
		int s1=min(s[0].id,s[n-1].id);
		int s2=max(s[0].id,s[n-1].id);
		if(flag) printf("Case %d: -1\n",sw++);
		else{
			printf("Case %d: %d\n",sw++,spfa(s1,s2,n));
		}
		
	}
	
	return 0;
 } 
 
  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值