HDOJ题目3440 House Man(差分约束)

House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2256    Accepted Submission(s): 896

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

Recommend

We have carefully selected several similar problems for you:  3439 3433 3442 3438 3437

题目大意:http://www.cnblogs.com/scau20110726/archive/2013/05/04/3059625.html

ac代码

#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
#include<stdlib.h>
#define INF 1<<30
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
using namespace std;
struct s
{
	int id,num;
}b[1010];
int n,m,vis[1010],head[1010],cnt,dis[1010],out[1010];
int cmp(const void *a,const void *b)
{
	return (*(struct s *)a).num-(*(struct s *)b).num;
}
struct node
{
	int u,v,w,next;
}edge[2020];
void add(int u,int v,int w)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
int spfa(int s)
{
	int i;
	for(i=1;i<=n;i++)
		dis[i]=INF;
	memset(vis,0,sizeof(vis));
	vis[s]=1;
	dis[s]=0;
	queue<int>q;
	q.push(s);
	memset(out,0,sizeof(out));
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		vis[u]=0;
		out[u]++;
		if(out[u]>n)
			return 0;
		for(i=head[u];i!=-1;i=edge[i].next)
		{
			int v=edge[i].v;
			if(dis[v]>dis[u]+edge[i].w)
			{
				dis[v]=dis[u]+edge[i].w;
				if(!vis[v])
				{
					vis[v]=1;
					q.push(v);
				}
			}
		}
	}
	return 1;
}
int main()
{
	int t,c=0;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		int i,j;
		memset(head,-1,sizeof(head));
		cnt=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&b[i].num);
			b[i].id=i;
		}
		qsort(b+1,n,sizeof(b[1]),cmp);
		for(i=2;i<=n;i++)
		{
			add(i,i-1,-1);
			if(b[i].id<b[i-1].id)
			{
				add(b[i].id,b[i-1].id,m);
			//	add(i,i-1,-1);
			}
			else
			{
				add(b[i-1].id,b[i].id,m);
			//	add(i-1,i,-1);
			}
		}
	//	for(i=1;i<=n;i++)
	//	{
	//		add(n+1,i,0);
	//	}
		int s=min(b[1].id,b[n].id);
		int t=max(b[1].id,b[n].id);
		int ans=spfa(s);
		printf("Case %d: ",++c);
		if(ans)
		{
			printf("%d\n",dis[t]);
		}
		else
			printf("-1\n");
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-28 22:50:51

HDOJ题目3440 House Man(差分约束)的相关文章

HDOJ 3666 THE MATRIX PROBLEM 差分约束

根据题意有乘除的关系,为了方便构图,用对数转化乘除关系为加减关系..... THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7486    Accepted Submission(s): 1914 Problem Description You have been given a matrix CN

POJ 题目3169 Layout(差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8354   Accepted: 4017 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

POJ1201 Intervals 【差分约束】

题目链接 POJ1201 题解 差分约束 令\(a[i]\)表示是否选择\(i\),\(s[i]\)表示\(a[i]\)的前缀和 对\(s[i] \quad i \in [-1,50000]\)分别建立一个点 首先有 \[s[i] - s[i - 1] \ge 0\] \[s[i] - s[i - 1] \le 1\] 然后就是限制条件 \[s[b] - s[a - 1] \ge c\] 然后就没了 用\(spfa\)跑最长路 由于题目保证有解,所以不会存在正环 复杂度上界是\(O(nm)\)的

差分约束算法总结

来自 https://blog.csdn.net/my_sunshine26/article/details/72849441 差分约束系统 一.概念 如果一个系统由n个变量和m个约束条件组成,形成m个形如ai-aj≤k的不等式(i,j∈[1,n],k为常数),则称其为差分约束系统. 二.引例 给定n个变量和m个不等式,每个不等式的形式为 x[i] - x[j] <= a[k] (0 <= i, j < n, 0 <= k < m, a[k]已知),求 x[i] - x[j]

BZOJ 3436: 小K的农场 差分约束

题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=3436 题解: 裸的差分约束: 1.a>=b+c  ->  b<=a-c  ->  d[v]<=d[u]+w  ->  建一条边从a到b,权值为-c 2.a<=b+c  ->  d[v]<=d[u]+w  -> 建一条边从b到a,权值为c 3.a==b  ->  d[v]<=d[u]+0&&d[u]<=d

【转载】差分约束

一直不知道差分约束是什么类型题目,最近在写最短路问题就顺带看了下,原来就是给出一些形如x-y<=b不等式的约束,问你是否满足有解的问题 好神奇的是这类问题竟然可以转换成图论里的最短路径问题,下面开始详细介绍下 比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图 由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较

UVALive 4885 Task 差分约束

题目链接:点击打开链接 题意: 有n个任务 m个限制条件 1.task i starts at least A minutes later than task j 表示 i - j >= A 2.task i starts within A minutes of the starting time of task j 表示 i - j <= A 问:每个任务开始的时间. 求一个任意解 思路: 差分约束,对于不等式形如: 点u,v : 常数C 有: u - v <= C 则从v->u

poj 3169 Layout (差分约束+Bellman )

题目链接:http://poj.org/problem?id=3169 题意:输入N, ML, MD, N默示有N个牛按1-N排成一排,ML,默示有ML行,每行输入A, B, D默示A牛和B牛最远间隔为D, MD默示有MD行,每行输入A,B,D默示A牛和B来间隔为D,求满足所有前提的1-N的最大间隔. 比较简单的差分约束,这个周周赛的A题 #include <iostream> #include <cstdlib> #include <cstdio> #include

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a