【差分约束/SPFA】 House Man HDU图论01最短路

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


#include <bits/stdc++.h>

using namespace std;

const int mn=1010;
const int inf=0x3f3f3f3f;

int N,D;

struct node
{
	int num;
	int h;
}n[mn];
bool cmp(node a,node b)
{
	return a.h<b.h;
}

int to[3*mn],cost[3*mn],nx[3*mn],fr[3*mn];

int B=-1;
void add(int a,int b,int c)
{
	B++;
	to[B]=b;
	cost[B]=c;
	nx[B]=fr[a];
	fr[a]=B;
}

int spfa(int S,int E)
{
	int d[mn];
	int ad[mn];
	bool vis[mn];
	queue<int>q;

	memset(ad,0,sizeof(ad));
	memset(vis,0,sizeof(vis));
	for(int i=0;i<N;i++)
		d[i]=inf;
	d[S]=0;

	q.push(S);
	vis[S]=1;
	ad[S]++;  //检查负环

	while(!q.empty())
	{
		int p=q.front();
		q.pop();
		vis[p]=0;

		for(int i=fr[p];i!=-1;i=nx[i])
		{
			if(d[to[i]]>d[p]+cost[i])
			{
				d[to[i]]=d[p]+cost[i];
				if(!vis[to[i]])
				{
					q.push(to[i]);
					vis[to[i]]=1;
					ad[to[i]]++;

					if(ad[to[i]]>(N+5))  //被放进队列中的次数
						return -1;  //存在负环
				}
			}
		}
	}
	return d[E];
}

int main()
{
	int T;scanf("%d",&T);
	for(int q=1;q<=T;q++)
	{
		B=-1;
		memset(fr,-1,sizeof(fr));

		scanf("%d%d",&N,&D);

		for(int i=0;i<N;i++)
		{
			n[i].num=i;
			scanf("%d",&n[i].h);
		}

		for(int i=0;i<N-1;i++)
			add(i+1,i,-1);

		sort(n,n+N,cmp);
		for(int i=0;i<N-1;i++)
		{
			int x=min(n[i].num,n[i+1].num);
			int y=max(n[i].num,n[i+1].num);
			add(x,y,D);
		}  //从左向右建图

		int S=min(n[0].num,n[N-1].num);
		int E=max(n[0].num,n[N-1].num);
		//有向的起点和终点

		printf("Case %d: ",q);
		printf("%d\n",spfa(S,E));
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值