现在的位置: 首页 > 综合 > 正文

HDU/HDOJ 3440 差分约束 2010年多校联合第一场

2017年11月23日 ⁄ 综合 ⁄ 共 3567字 ⁄ 字号 评论关闭

 

House Man

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

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
 
先开始完全木有思路
后来看了大牛们得思路。
豁然开朗。。。
 
建图的过程是:
相邻的两个点至少需要相差1m
然后高度相邻的两个点最多相差D米
这样就可以构建一个非常巧妙的图
 
我的代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<math.h>
#define maxn 10005
#define inf 0x7fffffff

using namespace std;

struct node
{
	int v;
	int len;
};
struct house
{
	int id;
	int high;
};
vector<node>map[maxn];
house h[maxn];
int n,d;

void init()
{
	int i;
	for(i=0;i<=n;i++)
		map[i].clear();
}

bool cmp(house a,house b)
{
	return a.high<b.high;
}

int spfa(int s,int e)
{
	int i,dis[maxn];
	bool used[maxn];
	int num[maxn];
	queue<int>q;
	memset(num,0,sizeof(num));
	memset(used,0,sizeof(used));
	for(i=0;i<=n;i++)
		dis[i]=inf;
	dis[s]=0;
	used[s]=true;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		used[u]=false;
		for(i=0;i<map[u].size();i++)
		{
			node p=map[u][i];
			if(dis[p.v]>dis[u]+p.len)
			{
				dis[p.v]=dis[u]+p.len;
				if(!used[p.v])
				{
					used[p.v]=true;
					num[p.v]++;
					if(num[p.v]>n)
						return -1;
					q.push(p.v);
				}
			}
		}
	}
	return dis[e];
}

int min(int a,int b)
{
	if(a>b)
		return b;
	else
		return a;
}

int max(int a,int b)
{
	if(a>b)
		return a;
	else
		return b;
}

int main()
{
	int i,t,T,ans,u,v;
	node p;
	scanf("%d",&T);
	for(t=1;t<=T;t++)
	{
		init();
		scanf("%d%d",&n,&d);
		for(i=1;i<=n;i++)
		{
			scanf("%d",&h[i].high);
			h[i].id=i;
		}
		for(i=1;i<=n-1;i++)
		{
			p.v=i;
			p.len=-1;
			map[i+1].push_back(p);
		}
		sort(h+1,h+1+n,cmp);
		for(i=1;i<=n-1;i++)
		{
			u=min(h[i].id,h[i+1].id);
			v=max(h[i].id,h[i+1].id);
			p.v=v;
			p.len=d;
			map[u].push_back(p);
		}
		u=min(h[1].id,h[n].id);
		v=max(h[1].id,h[n].id);
		ans=spfa(u,v);
		printf("Case %d: %d\n",t,ans);
	}
	return 0;
}

抱歉!评论已关闭.