hdu 3440 House Man

来源:互联网 发布:中国象棋分析软件 编辑:程序博客网 时间:2024/06/05 20:41

House Man

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


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
34 4 20 30 10 40 5 6 20 34 54 10 15 4 2 10 20 16 13
 

Sample Output
Case 1: 3Case 2: 3Case 3: -1
 

差分约束问题,关键是建图。

首先,题目中要求房子的排列必须是按照 input 时的顺序,所以就以第一个 输入的房子为原点,后面的依次放置房子的坐标位置为距离原点的长度。最后求最矮的房子到最高的房子的距离的最大值。

建立不等式依据为以第一个房子为原点,按照输入的顺序有 pos[ i ]  +1 <=  pos [ i+1 ],
然后按照高低排序后的顺序结合自己的坐标,有min(pos[ i ],pos[ i+1 ])+ d >= max( pos[ i ], pos[ i+1 ] )

另外一个注意的就是 如果起始点(最低房子)在终点(最高房子)后面,需要交换一下位置,因为根据不等式建立的第一条原则,会使结果为负值(一直按照输入顺序往前找)。

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <queue>#include <cmath>#include <algorithm>#include <climits>using namespace std;const int N = 1010;struct node{    int val,id;}node[N];struct edge{    int u,v,w,next;}edge[10010];int ahead[N],p=0,vis[N],dis[N],cnt[N];int spfa(int s,int t,int n){    int u,v,w;    queue<int> que;    memset(vis,0,sizeof(vis));    memset(cnt,0,sizeof(cnt));    for(int i = 0;i <= n;++i)    dis[i]=INT_MAX;    que.push(s);    dis[s]=0;    vis[s]=1;    ++cnt[s];    while(!que.empty()){        u = que.front();        que.pop();        vis[u]=0;        for(int i = ahead[u];i != -1;i=edge[i].next){            w=edge[i].w;            v=edge[i].v;            if(dis[v] > dis[u] + w){                dis[v] = dis[u] + w;                if(!vis[v]){                    que.push(v);                    vis[v] = 1;                    ++cnt[v];                    if(cnt[v] > n)                    {                        return -1;                    }                }            }        }    }    if( dis[t] == INT_MAX )        return -1;    return dis[t];}inline void addedge(int u,int v,int w){    edge[p].u=u,edge[p].v=v,edge[p].w=w,edge[p].next=ahead[u],ahead[u]=p++;    //edge[p].u=v,edge[p].v=u,edge[p].w=-1,edge[p].next=ahead[v],ahead[v]=p++;}inline void ainit(){    memset(ahead,-1,sizeof(ahead));    p=0;}bool cmp(struct node a,struct node b){    return a.val < b.val;}int main(){    int cas;    int n,d;    scanf("%d",&cas);    for(int cc = 1;cc <= cas;++cc){        scanf("%d%d",&n,&d);        ainit();        for(int i = 0;i < n;++i){            scanf("%d",&node[i].val);            node[i].id = i;        }        sort(node,node+n,cmp);        for(int i = 0; i < n-1; ++i){            int u = min(node[i].id,node[i+1].id);            int v = max(node[i].id,node[i+1].id);            addedge(u,v,d);            addedge(i+1,i,-1);        }        int ans;        if(node[0].id > node[n-1].id)            ans = spfa(node[n-1].id,node[0].id,n);        else            ans=spfa(node[0].id,node[n-1].id,n);        printf("Case %d: %d\n",cc,ans);    }    return 0;}


原创粉丝点击