UESTC Training for Graph Theory——L、House Man
来源:互联网 发布:时时彩单期计划软件 编辑:程序博客网 时间:2024/05/10 16:25
House Man
Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 43 Tried: 595
Description
In somewhere, 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<=50)
Each test case begins with a line containing two integers N (1 ≤ N ≤ 10000) and D (1 ≤ D ≤100000). 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
Source
2010 ACM-ICPC MU Training - Host by FZU
/*算法思想: 题意复杂,就不简述了 查分约束,建图是这样的: 1、每相邻的两个 house 的距离差不小于 1 2、第 i 大的 house 和第 i+1 大的 house 的距离不大于D 这样,我们建好图之后对 house 最小的点跑一遍最短路,记录下它到 house 最大的点的最短路,就是最终的答 案了。 由于边少点多,所以一定是用spfa跑最短路 这道题最麻烦的是判断无解的情况,刚开始用的每个点入队超过 n 次就无解(出现了负环),但是超时是肯定的 毕竟我们OJ的上的数据强的离谱。然后听老师们讲了dfs判重,还是不怎么搞懂,后来才知道原来spfa可以用栈来 实现,这样就方便判重了,遇到遍历过的点肯定是存在负环了,那么一定无解,不过貌似在没有负环的时候这样的 spfa跑的时间比用队列跑的spfa长*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define INF 0x7fffffff#define N 40000using namespace std;struct data1{ int st,en,val,next;} edge[N];struct data2{ int id,height;} house[N];int n,d,tot,head[N],min_id,max_id,dis[N];;bool vs[N];void add_edge(int st,int en,int val) //添加边{ edge[tot].st=st; edge[tot].en=en; edge[tot].val=val; edge[tot].next=head[st]; head[st]=tot++;}bool cmp(data2 a,data2 b){ return a.height<b.height;}bool dfs_spfa(int v) //用栈的dfs_spfa跑最短路{ if(vs[v]) return false; vs[v]=true; for(int i=head[v];i!=-1;i=edge[i].next) if(dis[edge[i].en]>dis[v]+edge[i].val) { dis[edge[i].en]=dis[v]+edge[i].val; if(!dfs_spfa(edge[i].en)) return false; } vs[v]=false; return true;}int main(){ int t; scanf("%d",&t); for(int ca=1;ca<=t;ca++) { memset(head,-1,sizeof(head)); tot=1; scanf("%d%d",&n,&d); for(int i=1;i<=n;i++) { scanf("%d",&house[i].height); house[i].id=i; } sort(house+1,house+n+1,cmp); for(int i=2;i<=n;i++) { int v1=min(house[i-1].id,house[i].id); int v2=max(house[i-1].id,house[i].id); add_edge(v1,v2,d); add_edge(i,i-1,-1); } printf("Case #%d: ",ca); min_id=min(house[1].id,house[n].id); max_id=max(house[1].id,house[n].id); memset(vs,0,sizeof(vs)); for(int i=1;i<=n;i++) dis[i]=INF; dis[min_id]=0; if(!dfs_spfa(min_id)) printf("-1\n"); else printf("%d\n",dis[max_id]); } return 0;}
- UESTC Training for Graph Theory——L、House Man
- 2014 UESTC Training for Graph Theory L
- UESTC Training for Graph Theory——A、Railway
- UESTC Training for Graph Theory——B、Asteroids
- UESTC Training for Graph Theory——c、Catenyms
- UESTC Training for Graph Theory——D、Distance Queries
- UESTC Training for Graph Theory——E、Distance Queries
- UESTC Training for Graph Theory——F、Instantaneous Transference
- UESTC Training for Graph Theory——G、Labeling Balls
- UESTC Training for Graph Theory——H、Redundant Paths
- UESTC Training for Graph Theory——I、War
- UESTC Training for Graph Theory——K、Blinking Stalkers
- UESTC Training for Graph Theory——M、Network
- 2014 UESTC Training for Graph Theory B
- 2014 UESTC Training for Graph Theory C
- 2014 UESTC Training for Graph Theory F
- 2014 UESTC Training for Graph Theory G
- 2014 UESTC Training for Graph Theory H
- 黑马程序员_学习日记25_异常
- 兰牛的总结——很有启发
- RAM和ROM和Flash ROM的区别
- 请放慢浮躁的脚步——毕业季的感悟
- c++ 虚函数
- UESTC Training for Graph Theory——L、House Man
- 黑马程序员_学习日记26_ASP.NET使用验证控件
- JQuery注册触发自定义事件
- 过了30岁,程序员该怎么办?
- FL2440_uCOS-II+ucGUI移植
- 黑马程序员_学习日记27_ASP.NET使用Rich控件
- 1天学习1个类 UIBarButtomItem 类 示例
- UESTC Training for Graph Theory——M、Network
- [Android]自定义移动网络接入点