博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ题目3440 House Man(差分约束)
阅读量:5246 次
发布时间:2019-06-14

本文共 4386 字,大约阅读时间需要 14 分钟。

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
 
Recommend
We have carefully selected several similar problems for you:            
 题目大意:http://www.cnblogs.com/scau20110726/archive/2013/05/04/3059625.html
ac代码
#include
#include
#include
#include
#include
#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"); } }

转载于:https://www.cnblogs.com/brucemengbm/p/6884512.html

你可能感兴趣的文章
Java ArrayList去重
查看>>
DashClock
查看>>
Hive 显示列名/表头
查看>>
windows查看与清理dns缓存
查看>>
[设计模式]外观模式
查看>>
linux下安装Composer
查看>>
Javascript format方法
查看>>
Javascript 绝对定位和相对定位
查看>>
ListView 应用5 - 数据库的增删改查及分页
查看>>
内置函数和匿名函数专区
查看>>
匿名内部类
查看>>
AJAX 表单提交 文件上传
查看>>
Redis拾遗(四)
查看>>
IOS多线程编程之锁的理解
查看>>
隐藏Apache版本号
查看>>
FPGA硬件加速
查看>>
Win7电脑开启局域网连接和共享过程中出现的"您可能没有权限使用网络资源"的解决办法...
查看>>
51. N-Queens
查看>>
vue父组件向子组件传递数据
查看>>
paip.hibernate list 返回位null的解决
查看>>