hdu 3440 House Man (去绝对值构图差分约束)

来源:互联网 发布:淘宝店铺邮费怎么设置 编辑:程序博客网 时间:2024/06/05 04:56


House Man

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


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
 

Author
jyd
 

Source
2010 ACM-ICPC Multi-University Training Contest(1)——Host by FZU

题意:

题意:有n个屋子,超人从最矮的屋子开始,依次跳下比当前屋子高且最接近当前高度的屋子(即按照屋子高度增序来跳),但超人跳跃还有一个水平距离限制D,他每次跳的水平距离<=D。现在给你每个屋子的高度是它们的相对位置,你不能改变屋子的相对位置,但是可以水平移动屋子,使得最矮的屋子和最高的屋子的水平距离最大。如果无论怎样移动,超人都无法跳到最后那个屋子则输出-1

思路:做差分约束首先判断先是否可以重合,不能就要有相差1的约束,x(i+1) - x(i) >=1,   因为要化为最短路 所以是  x(i) - x(i+1) <= -1,高度相近的两个坐标(设为xi,xj)相减  abs(xi-xj) <= d,   要想办法去掉绝对值, 那么规定一律id大的减去id小的,那么结果就是正的.如果高的在矮的后面,距离就是负数了,在前面就是整数了, 因为他只是要求绝对值<=d,所以我们建边都从小坐标往大坐标建边~这样d就是正的了,统一起来。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;const int maxn = 1e3 + 5;const int INF = 2e9;struct node{    int to, w;    node(){}    node(int tt, int ww) : to(tt) , w(ww){}}a[maxn];vector<node> v[maxn];int h, n, d, cnt[maxn], book[maxn], dis[maxn];int spfa(int s, int e){    memset(book, 0, sizeof(book));    for(int i = 1; i < maxn; i++) dis[i] = INF;    dis[s] = 0;    book[s] = 1;    cnt[s] = 1;    queue<int> q;    q.push(s);    while(!q.empty())    {        int u = q.front();        q.pop();        book[u] = 0;        for(int i = 0; i < v[u].size(); i++)        {            int to = v[u][i].to;            if(dis[u]+v[u][i].w < dis[to])            {                dis[to] = dis[u] + v[u][i].w;                if(!book[to])                {                    if(++cnt[to] > n)                        return -1;                    book[to] = 1;                    q.push(to);                }            }        }    }        return dis[e];}int cmp(node a, node b){    return a.to < b.to;}int main(){    int t, ca = 1;    cin >> t;    while(t--)    {        for(int i = 0; i < maxn; i++)            v[i].clear();        scanf("%d%d", &n, &d);        memset(cnt, 0, sizeof(cnt));        memset(dis, 0, sizeof(dis));        memset(book, 0, sizeof(book));        for(int i = 1; i <= n; i++)        {            scanf("%d", &a[i].to);            a[i].w = i;        }        sort(a+1, a+1+n, cmp);        for(int i = 2; i <= n; i++)        {            if(a[i].w > a[i-1].w)  v[a[i-1].w].push_back(node(a[i].w,d));            else  v[a[i].w].push_back(node(a[i-1].w,d));            v[i].push_back(node(i-1, -1));        }        int s = min(a[1].w, a[n].w);        int e = max(a[1].w, a[n].w);        printf("Case %d: %d\n", ca++, spfa(s,e));    }    return 0;}



原创粉丝点击