hdu3440 House Man 【差分约束系统】
House Man
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.
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.
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13
Case 2: 3
Case 3: -1
题意:有N个在一条直线上的房子, 每个房子有着不同的高度, 一个超人可以将这些房子左右移动但不能改变房子之间的相对位置.现在超人要从最矮的房子跳到刚好比他高的房子上面, 且每次跳的房子都要比当前房子要高.那么最后超人肯定会跳到最高的房子上面, 现在给出超人能够跳的最远距离, 问: 如何摆放这些房子, 使得超人能够经过所有的房子跳到最高的房子, 又要使最矮的房子和最高的房子之间的距离最远??(摘自讨论区)
题解:关键在于建图,不能乱建图,坐标小的才可以到达坐标大的,这样建图才可以。
然后就是线性差分约束了。
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<queue>
#define INF 2000000007
using namespace std; int n,m,S,T,Case;
int dis[],ins[],num[];
int cnt,head[],Next[],rea[],val[];
struct Node
{
int x,num;
}a[]; bool cmp(Node x,Node y)
{
return x.x<y.x;
}
void Spfa()
{
for (int i=;i<=n;i++)
dis[i]=INF,ins[i]=;
dis[S]=,ins[S]=;
queue<int>q;
q.push(S);
while(!q.empty())
{
int u=q.front();q.pop();
for (int i=head[u];i!=-;i=Next[i])
{
int v=rea[i],fee=val[i];
if (dis[v]>fee+dis[u])
{
dis[v]=fee+dis[u];
if (!ins[v])
{
ins[v]=;
q.push(v);
num[v]++;
if (num[v]>n)
{
dis[T]=-;
return;
}
}
}
}
ins[u]=;
}
}
void add(int u,int v,int fee)
{
Next[++cnt]=head[u];
head[u]=cnt;
rea[cnt]=v;
val[cnt]=fee;
}
int main()
{
int Cas;scanf("%d",&Cas);
while(Cas--)
{
cnt=;
memset(head,-,sizeof(head));
memset(num,,sizeof(num));
scanf("%d%d",&n,&m);
for (int i=;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].num=i;
if (i!=) add(i,i-,-);
}
sort(a+,a+n+,cmp);
for (int i=;i<n;i++)
{
int x=a[i].num,y=a[i+].num;
if (x>y) swap(x,y);
add(x,y,m);
}
S=a[].num;
T=a[n].num;
if (S>T) swap(S,T);
Spfa();
printf("Case %d: %d\n",++Case,dis[T]);
}
}
hdu3440 House Man 【差分约束系统】的更多相关文章
- UVA11478 Halum [差分约束系统]
https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...
- BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5395 Solved: 1750[Submit][Status ...
- ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)
当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方 ...
- POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
- Burn the Linked Camp(bellman 差分约束系统)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
- POJ 3169 Layout (差分约束系统)
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...
- POJ 3169 Layout 差分约束系统
介绍下差分约束系统:就是多个2未知数不等式形如(a-b<=k)的形式 问你有没有解,或者求两个未知数的最大差或者最小差 转化为最短路(或最长路) 1:求最小差的时候,不等式转化为b-a>= ...
- bzoj 2330 [SCOI2011]糖果(差分约束系统)
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3574 Solved: 1077[Submit][Status ...
随机推荐
- 四大开源协议比较:BSD、Apache、GPL、LGPL【转载】
四大开源协议原文链接 本文参考文献:http://www.fsf.org/licensing/licenses/ 现今存在的开源协议很多,而经过Open Source Initiative组织通过批准 ...
- zojDakar Rally(01背包)
01背包 加上每次更新解题数目最多 总用时最少 因为要保证用时最少,要先把时长由小到大排序. 没排序 WA了几小时..链接 #include <iostream> #include< ...
- xshell常用命令大全
xshell常用命令大全 (1)命令ls——列出文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当前目录下以字母a开头的所有文件 ls -l *. ...
- 设置webbrowser浏览器内核
var hklm = Microsoft.Win32.Registry.LocalMachine; var lmRun64 = hklm.OpenSubKey(@"SO ...
- (Android MVVM)使用Data Binding Library(2)
复习 上一篇学到了如何在layout.xml文件中增加元素,实现数据绑定,本篇接着学习. 事件处理 在layout.xml上绑定事件有两种方法,各有千秋. 1.方法引用 2.监听绑定 1.使用方法引用 ...
- Compiler 1.6.5 —1.6.7
Compiler 1.6.5 —1.6.7 Dynamic Scope Technically, any scoping policy is dynamic if it is based on fa ...
- JVM 优点与缺点的深入分析
Java 最初诞生的时候,它可以说是其他语言的进化版.不仅因为Java很简单,而且这一进化的语言还是一个可以运行第三方硬件字节码的虚拟机.它还是垃圾收集站,从而令存储管理和内核转储(core dump ...
- java内存查看与分析
业界有很多强大的java profile的工具,比如Jporfiler,yourkit,这些收费的东西我就不想说了,想说的是,其实java自己就提供了很多内存监控的小工具,下面列举的工具只是一小部分, ...
- hibernate4+spring4+struts2的Maven中的pom.xml文件的配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- .Net Core 真能令微软的.Net 跨平台“蔓延”?
什么是.Net .Net 本身就是基于公共语言基础架构(CLI)实现的平台独立的公共语言开发平台,只是自2006年成为规范以来的CLI,只有Windows自己支持罢了(mono除外).微软的.Net ...