hdu3440House Man【差分约束系统】

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
 

Recommend
We have carefully selected several similar problems for you:   3439  3433  3442  3438  3437 
 

这个小破题居然又是10年的多校,想想貌似做了好多10的,AC率近半也是醉了,据KIDx大牛说这个难度也不低,正在考虑差分约束是不是要就此收手

说题意,超人童鞋能在一些排成一排高矮不一的房子跳来跳去,高度的限制是只能从矮的跳到高的,长度的限制是不能超过k,问从最矮的跳到最高的,水平一共走多远,也就是说,他走的路线是由低到高的,小超人按着高度顺序来回跳,很明显能列出两个不等式,不放心瞅了一眼题解,没错啊,只有这两个约束啊,还有什么问题呢?既然是从小到大建的图,那么起点应该是最左边的,终点应该是最右边的。还不对,再改吧,尼玛,小于等于是最短路啊啊啊啊啊

/****************
hdu3440
2016.3.3
873MS	1808K	2176 B	C++
****************/
#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1005;
struct Edge
{
    int v,cost;
    Edge(int _v=0,int _cost=0):v(_v),cost(_cost){}
};
vector<Edge>E[maxn];
void addedge(int u,int v,int w)
{
    E[u].push_back(Edge(v,w));
}
bool vis[maxn];
int cnt[maxn];
int dist[maxn];
bool spfa(int start,int n)
{
    memset(vis,false,sizeof(vis));
    memset(dist,inf,sizeof(dist));
    vis[start]=true;
    dist[start]=0;
    queue<int>que;
    while(!que.empty()) que.pop();
    que.push(start);
    memset(cnt,0,sizeof(cnt));
    cnt[start]=1;
    while(!que.empty())
    {
        int u=que.front();
        que.pop();
        vis[u]=false;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            if(dist[v]>dist[u]+E[u][i].cost)
            {
                dist[v]=dist[u]+E[u][i].cost;
                if(!vis[v])
                {
                    vis[v]=true;
                    que.push(v);
                    if(++cnt[v]>n) return false;
                }
            }
        }
    }
    return true;
}
struct node
{
    int val,pos;
}nt[maxn];
bool cmp(node a,node b)
{
    return a.val<b.val;
}
int main()
{
  //  freopen("cin.txt","r",stdin);
    int t,n,d,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&d);
        for(int i=1;i<=n;i++) scanf("%d",&nt[i].val),nt[i].pos=i;
        sort(nt+1,nt+1+n,cmp);
        for(int i=0;i<=n;i++) E[i].clear();
        for(int i=1;i<n;i++)
        {
            addedge(i+1,i,-1);
            if(nt[i].pos<nt[i+1].pos)
                addedge(nt[i].pos,nt[i+1].pos,d);
            else addedge(nt[i+1].pos,nt[i].pos,d);
        }
        printf("Case %d: ",cas++);
        int minn,maxn;
        if(nt[1].pos<nt[n].pos)minn=nt[1].pos,maxn=nt[n].pos;
        else minn=nt[n].pos,maxn=nt[1].pos;
        if(!spfa(minn,n)) printf("-1\n");
        else printf("%d\n",dist[maxn]);
    }
    return 0;
}




你可能感兴趣的:(hdu3440House Man【差分约束系统】)