HDU3440 House Man【SPFA】【差分约束】

House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2160    Accepted Submission(s): 849

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

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

题目大意:在一条直线上有N个房子,每个房子的高度不一样,一个超人可以将这N个房子

左右移动,但是不能改变房子的相对位置。位置都为整数点。同一位置不能有两个以上的房

子两个超人从最矮的房子开始,跳到刚好比他所在房子高的房子上去,一直这样跳,每次跳

到的房子都比现在所在的房子高,而且每次跳的水平距离不能超过D米。问:如何将这些房

子进行合理的摆放(不改变相对位置的前提下),使得超人能够经过所有的房子跳到最高的房

子,又要使最矮的房子和最高的房子之间的距离最远?

输入说明:

第一行:先输入一个T,表示T组数据。

每组第一行:输入N D,表示N个房子,每次最多水平跳D米。

接下来N个数:表示从左到右N个房子的高度,输入顺序表示了房子的相对位置。

思路:差分约束系统。把这道题目分解来看。可以分解为两个条件。

第1条:两个房子之间水平距离至少为1(即不能再同一位置上)

第2条:超人每次最多跳D米

现将房子的高度和代表相对位置的序号存入结构体中,用结构体数组arr[]表示。

因为房子之间的水平距离至少为1,将条件转换为:arr[i].No - arr[i+1].No <= -1,

将边存入差分约束系统表示的图中。然后对结构体按房子高度从低到高排序,这样结

构体中房子的顺序就变为超人跳房子的顺序,因为超人每次最多水平跳D米,将条件

转化为:arr[i+1].No - arr[i].No <= D,将边存入差分约束系统表示的图中,然后源

点s为arr[1].No,终点t为arr[N].No,用SPFA来判断是否符合条件,符合输出Dist[t],

不符合输出"-1"。

注意:此题最主要是建边的时候,判断两者起点和终点的序号大小,让大的序号指向

小的序号。还有INF要设置的大一些。试了几次,发现0xfffffff不行,0x7fffffff就可以

了。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#define INF 0x7fffffff; //之前定义0xfffffff错误。
using namespace std;
const int MAXN = 1100;
const int MAXM = MAXN*MAXN;

struct EdgeNode
{
    int to;
    int w;
    int next;
}Edges[MAXM];

struct House
{
    int Hight;  //房子高度
    int No;     //房子序号
}arr[MAXN];

bool cmp(House a, House b)
{
    return a.Hight < b.Hight;
}
int Head[MAXN],Dist[MAXN],vis[MAXN],outque[MAXN],id;

void AddEdges(int u,int v,int w)
{
    Edges[id].to = v;
    Edges[id].w = w;
    Edges[id].next = Head[u];
    Head[u] = id++;
}
int SPFA(int s, int t,int N)
{
    memset(vis,0,sizeof(vis));
    memset(outque,0,sizeof(outque));
    for(int i = 0; i <= N; ++i)
        Dist[i] = INF;
    Dist[s] = 0;
    vis[s] = 1;
    queue<int> Q;
    Q.push(s);
    while( !Q.empty() )
    {
        int u = Q.front();
        Q.pop();
        vis[u] = 0;
        outque[u]++;
        if(outque[u] > N)
            return -1;
        for(int i = Head[u]; i != -1; i = Edges[i].next)
        {
            int temp = Dist[u] + Edges[i].w;
            if(temp < Dist[Edges[i].to])
            {
                Dist[Edges[i].to] = temp;
                if( !vis[Edges[i].to])
                {
                    vis[Edges[i].to] = 1;
                    Q.push(Edges[i].to);
                }
            }
        }
    }
    return Dist[t];
}

int main()
{
    int T,N,D,kase = 0;
    scanf("%d", &T);
    while(T--)
    {
        memset(Head,-1,sizeof(Head));
        scanf("%d%d", &N, &D);
        id = 0;
        for(int i = 1; i <= N; ++i)
        {
            scanf("%d", &arr[i].Hight);
            arr[i].No = i;
            if(i != N)
                AddEdges(i+1,i,-1);
        }

        sort(arr+1,arr+N+1,cmp);
        for(int i = 1; i < N; ++i)
        {
            int u = arr[i].No;
            int v = arr[i+1].No;
            if(u > v)
                swap(u,v);
            AddEdges(u,v,D);
        }
        int u = arr[1].No;
        int v = arr[N].No;
        if(u > v)
            swap(u,v);
        printf("Case %d: %d\n",++kase,SPFA(u,v,N));
    }

    return 0;
}
时间: 2025-01-20 00:30:29

HDU3440 House Man【SPFA】【差分约束】的相关文章

codevs 1183 泥泞的道路 (二分+SPFA+差分约束)

/* 二分答案(注意精度) 对于每一个答案 有(s1+s2+s3...)/(t1+t2+t3...)>=ans 时符合条件 这时ans有变大的空间 对于上述不等式如果枚举每一条路显得太暴力 化简一下变成 :s1-t1*ans+s2-t2*ans+s3-t3*ans...>=0 差分约束跑最长路 如果dis[n]>0 或者有正环 (开始这个忘掉了)ans就合法 */ #include<iostream> #include<cstdio> #include<cs

Candies---hdu3159(spfa+差分约束)

题目链接:http://poj.org/problem?id=3159 题意:有n个小孩,m个关系格式是A B C 表示小孩 B 的糖果数最多比小孩A多C个,相当于B-A<=C; 有m个这样的关系最后求小孩n比小孩1最多多几个糖果: 差分约束: 比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值, 我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图   由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc

poj1201——差分约束,spfa

poj1201——差分约束,spfa Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 22553   Accepted: 8530 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of inte

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

POJ1201 Intervals【SPFA】【差分约束】

Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22307 Accepted: 8413 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points

poj3159 差分约束 spfa

1 //Accepted 2692 KB 1282 ms 2 //差分约束 -->最短路 3 //TLE到死,加了输入挂,手写queue 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std;

Candies(差分约束_栈+SPFA)

CandiesCrawling in process... Crawling failed Time Limit:1500MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher b

poj3159——Candies(差分约束+SPFA堆栈)

Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse's class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and ofte

(简单) POJ 3169 Layout,差分约束+SPFA。

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe