hdu3440 House Man 【差分约束系统】

House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2056    Accepted Submission(s): 811



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

 题意:有N个在一条直线上的房子, 每个房子有着不同的高度, 一个超人可以将这些房子左右移动但不能改变房子之间的相对位置.现在超人要从最矮的房子跳到刚好比他高的房子上面, 且每次跳的房子都要比当前房子要高.那么最后超人肯定会跳到最高的房子上面, 现在给出超人能够跳的最远距离, 问: 如何摆放这些房子, 使得超人能够经过所有的房子跳到最高的房子, 又要使最矮的房子和最高的房子之间的距离最远??(摘自讨论区)

题解:关键在于建图,不能乱建图,坐标小的才可以到达坐标大的,这样建图才可以。

然后就是线性差分约束了。

 1 #include<cstring>
 2 #include<cmath>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<cstdio>
 6 #include<queue>
 7 #define INF 2000000007
 8 using namespace std;
 9 
10 int n,m,S,T,Case;
11 int dis[1007],ins[1007],num[1007];
12 int cnt,head[1007],Next[10007],rea[10007],val[10007];
13 struct Node
14 {
15     int x,num;
16 }a[1007];
17 
18 bool cmp(Node x,Node y)
19 {
20     return x.x<y.x;
21 }
22 void Spfa()
23 {
24     for (int i=1;i<=n;i++)
25         dis[i]=INF,ins[i]=0;
26     dis[S]=0,ins[S]=1;
27     queue<int>q;
28     q.push(S);
29     while(!q.empty())
30     {
31         int u=q.front();q.pop();
32         for (int i=head[u];i!=-1;i=Next[i])
33         {
34             int v=rea[i],fee=val[i];
35             if (dis[v]>fee+dis[u])
36             {
37                 dis[v]=fee+dis[u];
38                 if (!ins[v])
39                 {
40                     ins[v]=1;
41                     q.push(v);
42                     num[v]++;
43                     if (num[v]>n)
44                     {
45                         dis[T]=-1;
46                         return;
47                     }
48                 }
49             }
50         }
51         ins[u]=0;
52     }
53 }
54 void add(int u,int v,int fee)
55 {
56     Next[++cnt]=head[u];
57     head[u]=cnt;
58     rea[cnt]=v;
59     val[cnt]=fee;
60 }
61 int main()
62 {
63     int Cas;scanf("%d",&Cas);
64     while(Cas--)
65     {
66         cnt=0;
67         memset(head,-1,sizeof(head));
68         memset(num,0,sizeof(num));
69         scanf("%d%d",&n,&m);
70         for (int i=1;i<=n;i++)
71         {
72             scanf("%d",&a[i].x);
73             a[i].num=i;
74             if (i!=1) add(i,i-1,-1);
75         }
76         sort(a+1,a+n+1,cmp);
77         for (int i=1;i<n;i++)
78         {
79             int x=a[i].num,y=a[i+1].num;
80             if (x>y) swap(x,y);
81             add(x,y,m);
82         }
83         S=a[1].num;
84         T=a[n].num;
85         if (S>T) swap(S,T);
86         Spfa();
87         printf("Case %d: %d
",++Case,dis[T]);
88     }
89 }
原文地址:https://www.cnblogs.com/fengzhiyuan/p/7766945.html