现在的位置: 首页 > 综合 > 正文

hdu 3440 House Man 差分约束

2013年03月03日 ⁄ 综合 ⁄ 共 3892字 ⁄ 字号 评论关闭
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

 

 //

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=10000+10;
struct edge
{
    int t,w;
    int next;
};
int p[maxn];
edge G[maxn*6];
int l;
void init()
{
    l=0;
    memset(p,-1,sizeof(p));
}
void addedge(int u,int t,int w,int l)
{
    G[l].t=t;
    G[l].w=w;
    G[l].next=p[u];
    p[u]=l;
}
int V,E;
bool v[maxn];
int dis[maxn];
int que[maxn],front,rear;
int inque[maxn];
bool vis[maxn];
bool spfa(int s0)
{
    memset(vis,0,sizeof(vis));
    memset(inque,0,sizeof(inque));
    front=rear=0;
    for(int i=1;i<=V;i++)
    {
        dis[i]=(1<<30);
    }dis[s0]=0;
    que[rear++]=s0;inque[s0]++;vis[s0]=true;
    while(front!=rear)
    {
        int u=que[front++];
        if(front==maxn) front=0;
        vis[u]=0;
        for(int i=p[u];i!=-1;i=G[i].next)
        {
            int t=G[i].t,w=G[i].w;
            if(dis[t]>dis[u]+w)
            {
                dis[t]=dis[u]+w;
                if(vis[t]==0)
                {
                    que[rear++]=t;
                    if(rear==maxn) rear=0;
                    vis[t]=1;
                    inque[t]++;
                    if(inque[t]>V) return false;
                }
            }
        }
    }
    return true;
}
struct Node
{
    int idx;
    int h;
};
Node house[maxn];
bool cmp(Node h,Node k)
{
    return h.h<k.h;
}
int main()
{
    int ci,pl=1;scanf("%d",&ci);
    while(ci--)
    {
        int D;
        scanf("%d%d",&V,&D);
        init();
        for(int i=1;i<=V;i++)
        {
            scanf("%d",&house[i].h);
            house[i].idx=i;
        }
        sort(house+1,house+1+V,cmp);
        for(int i=1;i<V;i++)
        {
            int md=max(house[i+1].idx,house[i].idx);
            int nd=min(house[i+1].idx,house[i].idx);
            addedge(nd,md,D,l++);
            addedge(i+1,i,-1,l++);
        }
        int md=max(house[V].idx,house[1].idx);
        int nd=min(house[V].idx,house[1].idx);
        if(spfa(nd))
        {
            int cnt=dis[md];
            printf("Case %d: %d\n",pl++,cnt);
        }
        else printf("Case %d: -1\n",pl++);
    }
    return 0;
}

 

 

抱歉!评论已关闭.