UESTC Training for Graph Theory——L、House Man

House Man

Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 43 Tried: 595

Description

In somewhere, 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<=50)
Each test case begins with a line containing two integers N (1 ≤ N ≤ 10000) and D (1 ≤ D ≤100000). 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

Source

2010 ACM-ICPC MU Training - Host by FZU

 

/*算法思想:
  题意复杂,就不简述了
  查分约束,建图是这样的:
  1、每相邻的两个 house 的距离差不小于 1
  2、第 i 大的 house 和第 i+1 大的 house 的距离不大于D
  这样,我们建好图之后对 house 最小的点跑一遍最短路,记录下它到 house 最大的点的最短路,就是最终的答
  案了。
  由于边少点多,所以一定是用spfa跑最短路
  这道题最麻烦的是判断无解的情况,刚开始用的每个点入队超过 n 次就无解(出现了负环),但是超时是肯定的
  毕竟我们OJ的上的数据强的离谱。然后听老师们讲了dfs判重,还是不怎么搞懂,后来才知道原来spfa可以用栈来
  实现,这样就方便判重了,遇到遍历过的点肯定是存在负环了,那么一定无解,不过貌似在没有负环的时候这样的
  spfa跑的时间比用队列跑的spfa长
*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x7fffffff
#define N 40000
using namespace std;
struct data1
{
    int st,en,val,next;
} edge[N];
struct data2
{
    int id,height;
} house[N];
int n,d,tot,head[N],min_id,max_id,dis[N];;
bool vs[N];
void add_edge(int st,int en,int val)  //添加边
{
    edge[tot].st=st;
    edge[tot].en=en;
    edge[tot].val=val;
    edge[tot].next=head[st];
    head[st]=tot++;
}
bool cmp(data2 a,data2 b)
{
    return a.height<b.height;
}
bool dfs_spfa(int v)  //用栈的dfs_spfa跑最短路
{
    if(vs[v]) return false;
    vs[v]=true;
    for(int i=head[v];i!=-1;i=edge[i].next)
        if(dis[edge[i].en]>dis[v]+edge[i].val)
        {
            dis[edge[i].en]=dis[v]+edge[i].val;
            if(!dfs_spfa(edge[i].en)) return false;
        }
    vs[v]=false;
    return true;
}
int main()
{
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        memset(head,-1,sizeof(head));
        tot=1;
        scanf("%d%d",&n,&d);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&house[i].height);
            house[i].id=i;
        }
        sort(house+1,house+n+1,cmp);
        for(int i=2;i<=n;i++)
        {
            int v1=min(house[i-1].id,house[i].id);
            int v2=max(house[i-1].id,house[i].id);
            add_edge(v1,v2,d);
            add_edge(i,i-1,-1);
        }
        printf("Case #%d: ",ca);
        min_id=min(house[1].id,house[n].id);
        max_id=max(house[1].id,house[n].id);
        memset(vs,0,sizeof(vs));
        for(int i=1;i<=n;i++)
            dis[i]=INF;
        dis[min_id]=0;
        if(!dfs_spfa(min_id))
            printf("-1\n");
        else
            printf("%d\n",dis[max_id]);
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
互联网络程序设计是指在互联网上进行程序开发和设计的过程。UESTC则是我国的一所著名高校——电子科技大学。 互联网络程序设计 uestc包含了两个主要的方面:互联网络和程序设计。互联网络是指将多个计算机网络通过通信链路互相连接起来,实现信息共享和资源共享的网络系统。程序设计是指根据需求和目标,通过编写代码和设计算法,实现计算机程序的过程。 互联网络程序设计 uestc的学习内容主要包括以下几个方面: 1. 网络知识:学习互联网络的基本概念、原理和协议,如TCP/IP协议、HTTP协议等。掌握网络编程的基本技术,能够编写网络应用程序。 2. 数据通信:学习数据通信的基本原理和技术,包括数据传输的方式、数据压缩和加密等。了解网络安全和数据保护的基本知识。 3. 程序设计:学习编程语言和开发工具,如Java、C++和Python等。掌握常用的编程技巧和方法,能够设计和实现复杂的网络应用程序。 4. Web开发:学习Web开发的基本知识和技术,包括HTML、CSS、JavaScript等。能够设计和实现交互式的Web应用程序。 5. 数据库技术:学习数据库的基本原理和技术,如SQL语言和数据库管理系统。能够设计和管理数据库,实现数据的存储和检索。 通过学习互联网络程序设计 uestc,可以掌握互联网应用开发的基本技能,具备设计和实现网络应用程序的能力。这对于目前互联网行业的人才需求来说是非常重要的,也为学生提供了广阔的就业和创业机会。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值