思路:读懂题之后的差分约束
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
#define inf 1e9
struct Node
{
int h,id;
}hou[maxn];
bool cmp(Node a,Node b){return a.h<b.h;}
vector<pair<int,int> >e[maxn];
int inq[maxn],d[maxn],cnt[maxn],n,m,s,t;
int spfa()
{
for(int i = 0;i<=n;i++)
d[i]=inf;
memset(inq,0,sizeof(inq));
memset(cnt,0,sizeof(cnt));
queue<int>q;
q.push(s);
d[s]=0;
while(!q.empty())
{
int u = q.front();
q.pop();
inq[u]=0;
for(int i = 0;i<e[u].size();i++)
{
int v = e[u][i].first;
if(d[v]>d[u]+e[u][i].second)
{
d[v]=d[u]+e[u][i].second;
if(!inq[v])
{
inq[v]=1;
q.push(v);
if(++cnt[v]>n)
return -1;
}
}
}
}
return d[t];
}
int main()
{
int T,cas=1;
scanf("%d",&T);
int d;
while(T--)
{
scanf("%d%d",&n,&d);
for(int i = 0;i<=n;i++)
e[i].clear();
for(int i = 1;i<=n;i++)
{
scanf("%d",&hou[i].h);
hou[i].id = i;
if(i!=n)
e[i+1].push_back(make_pair(i,-1));
}
sort(hou+1,hou+1+n,cmp);
for(int i = 1;i<n;i++)
{
int u = hou[i].id;
int v = hou[i+1].id;
if(u>v)swap(u,v);
e[u].push_back(make_pair(v,d));
}
printf("Case %d: ",cas++);
s = hou[1].id;
t = hou[n].id;
if(s>t)swap(s,t);
printf("%d\n",spfa());
}
}
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