House Man(HDU 3440)---线性约束 - 最短路模型 + 判负环

题目链接

题目描述

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:

  • All houses are to be moved along a one-dimensional path.
  • Houses must be moved at integer locations along the path, with no two houses at the same location.
  • 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.
  • 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.

输入格式

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.

输出格式

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.

输入样例

3
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13

输出样例

Case 1: 3
Case 2: 3
Case 3: -1

分析

线性约束模板题,主要是按照编号顺序的原则进行建图,有向边的建立方向为编号小指向编号大,另外对于最矮和最高房子的编号,将其中较小值赋给起点,较大值赋给终点,这样才能保证在求最短路的时候,除非不存在解,否则始终能从起点走到终点,具体参考代码。
对于差分约束不太懂的同学可以参考这个大佬写的文章,写的贼好!!!—传送门

源程序

SPFA算法

#include <bits/stdc++.h>
#define MAXN 1005
using namespace std;
struct Node{
	int id,height;
	bool operator <(const Node tmp)const{
		return height<tmp.height;
	}
}a[MAXN];
struct Edge{
	int v,w,next;
	Edge(){};
	Edge(int _v,int _w,int _next){
		v=_v,w=_w,next=_next;
	};
}edge[MAXN*MAXN];
int EdgeCount,head[MAXN];
int n,d,h,s,t,dis[MAXN],ven[MAXN],nums[MAXN];
void addEdge(int u,int v,int w)
{
	edge[++EdgeCount]=Edge(v,w,head[u]);
	head[u]=EdgeCount;
} 
bool SPFA()
{
	queue<int> q;
	memset(dis,0x3f,sizeof(dis));
	memset(ven,0,sizeof(ven));
	memset(nums,0,sizeof(nums));
	dis[s]=0;
	ven[s]=nums[s]=1;
	q.push(s);
	while(!q.empty()){
		int u=q.front();q.pop();
		ven[u]=0;
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v,w=edge[i].w;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				if(!ven[v]){
					q.push(v);
					ven[v]=1;
					nums[v]++;
					if(nums[v]>n)return false;
				}
			}
		}
	}
	return true;
} 
int main()
{
	int k,cas=0;
	scanf("%d",&k);
	while(k--){
		memset(head,0,sizeof(head));
		EdgeCount=0;
		scanf("%d%d",&n,&d);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i].height);
			a[i].id=i;
		}
		sort(a+1,a+n+1);
		for(int i=1;i<n;i++){	//按d建边 
			int u=a[i].id,v=a[i+1].id;
			if(u>v)addEdge(v,u,d);
			else addEdge(u,v,d);
		}
		for(int i=1;i<n;i++)	//按编号顺序建边 
			addEdge(i+1,i,-1);
		if(a[n].id>a[1].id)s=a[1].id,t=a[n].id;
		else s=a[n].id,t=a[1].id;
		if(SPFA())printf("Case %d: %d\n",++cas,dis[t]);
		else printf("Case %d: %d\n",++cas,-1);
	}
}

SPFA算法之SLF优化

#include <bits/stdc++.h>
#define MAXN 1005
using namespace std;
struct Node{
	int id,height;
	bool operator <(const Node tmp)const{
		return height<tmp.height;
	}
}a[MAXN];
struct Edge{
	int v,w,next;
	Edge(){};
	Edge(int _v,int _w,int _next){
		v=_v,w=_w,next=_next;
	};
}edge[MAXN*MAXN];
int EdgeCount,head[MAXN];
int n,d,h,s,t,dis[MAXN],ven[MAXN],nums[MAXN];
void addEdge(int u,int v,int w)
{
	edge[++EdgeCount]=Edge(v,w,head[u]);
	head[u]=EdgeCount;
} 
bool SPFA()
{
	deque<int> q;
	memset(dis,0x3f,sizeof(dis));
	memset(ven,0,sizeof(ven));
	memset(nums,0,sizeof(nums));
	dis[s]=0;
	ven[s]=nums[s]=1;
	q.push_back(s);
	int k;
	while(k=q.size()){
		int u=q.front();q.pop_front();
		ven[u]=0;
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v,w=edge[i].w;
			if(dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				if(!ven[v]){
					if(k>1&&dis[v]<dis[q.front()]) q.push_front(v);
					else q.push_back(v);
					ven[v]=1;
					nums[v]++;
					if(nums[v]>n)return false;
				}
			}
		}
	}
	return true;
} 
int main()
{
	int k,cas=0;
	scanf("%d",&k);
	while(k--){
		memset(head,0,sizeof(head));
		EdgeCount=0;
		scanf("%d%d",&n,&d);
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i].height);
			a[i].id=i;
		}
		sort(a+1,a+n+1);
		for(int i=1;i<n;i++){	//按d建边 
			int u=a[i].id,v=a[i+1].id;
			if(u>v)addEdge(v,u,d);
			else addEdge(u,v,d);
		}
		for(int i=1;i<n;i++)	//按编号顺序建边 
			addEdge(i+1,i,-1);
		if(a[n].id>a[1].id)s=a[1].id,t=a[n].id;
		else s=a[n].id,t=a[1].id;
		if(SPFA())printf("Case %d: %d\n",++cas,dis[t]);
		else printf("Case %d: %d\n",++cas,-1);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值