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 ...
随机推荐
- Oracle报错:"ORA-18008: 无法找到 OUTLN 方案 "的解决方案
Oracle报错:"ORA-18008: 无法找到 OUTLN 方案 "的解决方案 今天连接到Oracle报错:ORA-18008: 无法找到 OUTLN 方案,前天还用得 ...
- GUI初步和frame&panel
java的话这个GUI其实不是什么重点,但我们也要学习,重点是学习这种图形编程的思路. java里面对于图形的一些类都封装在了AWT和它的一些子包里.AWT(抽象窗口开发包) 当 ...
- HUST 1698 - 电影院 组合数学 + 分类思想
http://acm.hust.edu.cn/problem/show/1698 题目就是要把一个数n分成4段,其中中间两段一定要是奇数. 问有多少种情况. 分类, 奇数 + 奇数 + 奇数 + 奇数 ...
- String的用法——其他功能
package cn.itcast_06; /* String类的其他功能: 替换功能: String replace(char old,char new) String replace(String ...
- SpringBoot2.1.3修改tomcat参数支持请求特殊符号
最近遇到一个问题,比如GET请求中,key,value中带有特殊符号,请求会报错,见如下URL: http://xxx.xxx.xxx:8081/aaa?key1=val1&a.[].id=1 ...
- CF916C Jamie and Interesting Graph
思路:构造 实现: #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n ...
- SQL 触发器-如何查看当前数据库中有哪些触发器
在查询分析器中运行: use 数据库名goselect * from sysobjects where xtype='TR' sysobjects 保存着数据库的对象,其中 xtype 为 TR 的记 ...
- java 解析四则混合运算表达式并计算结果
package ch8; import java.util.LinkedList; import java.util.List; import java.util.Stack; /** * 四则混合运 ...
- 语音行业技术领先者Nuance上海诚招ASR/NLP研发工程师和软件工程师
Nuance is a leading provider of voice and language solutions for businesses and consumers around the ...
- 迅为iTOP-4418嵌入式开发板初体验
iTOP-4418开发板预装 Android4.4.4 系统, 支持9.7 寸.7 寸.4.3 寸屏幕. 参数:核心板参数 尺寸 50mm*60mm高度 核心板连接器为1.5mmCPU ARM Cor ...