【bzoj1731】Layout 排队布局
1731: [Usaco2005 dec]Layout 排队布局
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 868 Solved: 495
[Submit][Status][Discuss]
Description
Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate). Some cows like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of ML (1 <= ML <= 10,000) constraints describes which cows like each other and the maximum distance by which they may be separated; a subsequent list of MD constraints (1 <= MD <= 10,000) tells which cows dislike each other and the minimum distance by which they must be separated. Your job is to compute, if possible, the maximum possible distance between cow 1 and cow N that satisfies the distance constraints.
当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些。FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食。奶牛排在队伍中的顺序和它们的编号是相同的。因为奶牛相当苗条,所以可能有两头或者更多奶牛站在同一位置上。即使说,如果我们想象奶牛是站在一条数轴上的话,允许有两头或更多奶牛拥有相同的横坐标。一些奶牛相互间存有好感,它们希望两者之间的距离不超过一个给定的数L。另一方面,一些奶牛相互间非常反感,它们希望两者间的距离不小于一个给定的数D。给出ML条关于两头奶牛间有好感的描述,再给出MD条关于两头奶牛间存有反感的描述。(1<=ML,MD<=10000,1<=L,D<=1000000)你的工作是:如果不存在满足要求的方案,输出-1;如果1号奶牛和N号奶牛间的距离可以任意大,输出-2;否则,计算出在满足所有要求的情况下,1号奶牛和N号奶牛间可能的最大距离。
Input
* Line 1: Three space-separated integers: N, ML, and MD. * Lines 2..ML+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at most D (1 <= D <= 1,000,000) apart. * Lines ML+2..ML+MD+1: Each line contains three space-separated positive integers: A, B, and D, with 1 <= A < B <= N. Cows A and B must be at least D (1 <= D <= 1,000,000) apart.
Output
* Line 1: A single integer. If no line-up is possible, output -1. If cows 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between cows 1 and N.
Sample Input
1 3 10
2 4 20
2 3 3
INPUT DETAILS:
There are 4 cows. Cows #1 and #3 must be no more than 10 units
apart, cows #2 and #4 must be no more than 20 units apart, and cows
#2 and #3 dislike each other and must be no fewer than 3 units apart.
Sample Output
四只牛分别在0,7,10,27.
HINT
Source
题意:
$N$头牛排队,有一些牛希望自己和另一只牛距离不超过$D$(日久生情),
还有一些牛希望自己和另一只牛距离不小于$D$(因爱生恨)。
问满足所有条件时$1$号牛和$N$号牛的距离最远是多少?
题解:
看到形如三角形不等式的约束条件基本就可以想差分约束的建图了。
(毕竟我好像也没学过多少处理三角形不等式的方法吧)
询问距离最大值一定是计算最短路的,那么只需要将不等式转化成$s_i-s_j\leq w$的形式即可。
转换完成后跑$spfa$,这道题跟板有一点小区别:
- 当图上存在负环时,肯定不能满足所有条件,输出$-1$。
- 当$dis_n=\infty$时,$n$与$1$无约束,即可以任意大,输出$-2$。
- 否则,输出$dis_n$。
代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue> using namespace std;
#define MAXN 100005
#define MAXM 500005
#define INF 0x3f3f3f3f
#define ll long long int hd[MAXN],to[MAXN<<],cnt;
int nxt[MAXN<<],cst[MAXN<<];
int dis[MAXN];bool vis[MAXN]; inline int read(){
int x=,f=;
char c=getchar();
for(;!isdigit(c);c=getchar())
if(c=='-')
f=-;
for(;isdigit(c);c=getchar())
x=x*+c-'';
return x*f;
} inline void addedge(int u,int v,int w){
to[++cnt]=v,cst[cnt]=w;
nxt[cnt]=hd[u],hd[u]=cnt;
return;
} inline bool spfa_dfs(int u){
vis[u]=;
for(int i=hd[u];i;i=nxt[i]){
int v=to[i];
if(dis[v]>dis[u]+cst[i]){
if(vis[v]) return ;
dis[v]=dis[u]+cst[i];
if(!spfa_dfs(v)) return ;
}
}
vis[u]=;
return ;
} inline void spfa_bfs(int s){
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
queue<int> q;q.push(s);
dis[s]=;vis[s]=;
while(!q.empty()){
int u=q.front();
q.pop();vis[u]=;
for(int i=hd[u];i;i=nxt[i]){
int v=to[i];
if(dis[v]>dis[u]+cst[i]){
dis[v]=dis[u]+cst[i];
if(!vis[v])
q.push(v),vis[v]=;
}
}
}
return;
} int main(){
int N=read(),M1=read(),M2=read();
for(int i=;i<=M1;i++){
int u=read(),v=read(),w=read();
if(u>v) swap(u,v);
addedge(u,v,w);
}
for(int i=;i<=M2;i++){
int u=read(),v=read(),w=read();
if(u>v) swap(u,v);
addedge(v,u,-w);
}
for(int i=;i<=N;i++) addedge(i,i-,);
memset(dis,,sizeof(dis));
dis[]=;
if(!spfa_dfs()){
printf("-1\n");
return ;
}
spfa_bfs();
if(dis[N]>=INF){
printf("-2\n");
return ;
}
printf("%d\n",dis[N]);
return ;
}
【bzoj1731】Layout 排队布局的更多相关文章
- 【BZOJ1731】[Usaco2005 dec]Layout 排队布局 差分约束
[BZOJ1731][Usaco2005 dec]Layout 排队布局 Description Like everyone else, cows like to stand close to the ...
- 1731: [Usaco2005 dec]Layout 排队布局*
1731: [Usaco2005 dec]Layout 排队布局 题意: n头奶牛在数轴上,不同奶牛可以在同个位置处,编号小的奶牛必须在前面.m条关系,一种是两头奶牛距离必须超过d,一种是两头奶牛距离 ...
- 排队(BZOJ1731:[Usaco2005 dec]Layout 排队布局)
[问题描述] Czy喜欢将他的妹子们排成一队.假设他拥有N只妹纸,编号为1至N.Czy让他们站成一行,等待自己来派送营养餐.这些妹纸按照编号大小排列,并且由于它们都很想早点吃饭,于是就很可能出现多只妹 ...
- [bzoj1731] [Usaco2005 dec]Layout 排队布局
差分约束系统...因为题目要求的是1和n的最大距离所以这题就跑最长路.. 对于互相反感的牛(i与j互相反感,彼此距离至少为len,i<j),就有dis[j]-dis[i]>=len.就加一 ...
- BZOJ1731:[USACO]Layout 排队布局(差分约束)
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- [Usaco2005 dec]Layout 排队布局 差分约束
填坑- 差分约束一般是搞一个不等式组,求xn-x1的最大最小值什么的,求最大值就转化成xa<=xb+w这样的,然后建图跑最短路(这才是最终约束的),举个例子 x1<=x0+2x2<= ...
- bzoj 1731: [Usaco2005 dec]Layout 排队布局 ——差分约束
Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相 ...
- 【BZOJ】1731: [Usaco2005 dec]Layout 排队布局
[题意]给定按编号顺序站成一排的牛,给定一些约束条件如两牛距离不小于或不大于某个值,求1和n的最大距离.无解输出-1,无穷解输出-2. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...
- BZOJ 1731: [Usaco2005 dec]Layout 排队布局
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
随机推荐
- IPFS 到底是怎么工作的?
简介 我们知道,一个存储服务,最基本的功能就是存和取.IPFS 中提供了这两种语义,那就是 add 和 get 操作. 在 IPFS 系统中执行 add 操作,就是执行了一次存操作,放在网络的概念里, ...
- codeforces B. Convex Shape 解题报告
题目链接:http://codeforces.com/problemset/problem/275/B 题目内容:给出一个n * m 大小的grid,上面只有 black 和 white 两种颜色填充 ...
- Python GIL、线程锁、信号量及事件
GIL是什么? GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念.就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代码.有名的编 ...
- UniCode转码
<script type="text/javascript"> var GB2312UnicodeConverter = { ToUnicode: function ( ...
- keras中的Flatten和Reshape
最近在看SSD源码的时候,就一直不理解,在模型构建的时候如果使用Flatten或者是Merge层,那么整个数据的shape就发生了变化,那么还可以对应起来么(可能你不知道我在说什么)?后来不知怎么的, ...
- Python: PS 图像调整--饱和度调整
本文用 Python 实现 PS 图像调整中的饱和度调整算法,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/detail ...
- bootstrap学习大纲
bootstrap 学习分三部分,分别是 css样式,css组件,js插件. 下面介绍三部分分别要学习的内容: 1.css样式:栅格系统,排版,代码,表格,表单,按钮,图片,辅助类,响应式工具. 2. ...
- kitti数据集标定文件解析
1.kitti数据采集平台 KITTI数据集的数据采集平台装配有2个灰度摄像机,2个彩色摄像机,一个Velodyne64线3D激光雷达,4个光学镜头,以及1个GPS导航系统.图示为传感器的配置平面图, ...
- c语言里如何调用汇编里的变量?
c语言里如何调用汇编里的变量? 汇编语言:是声明全局变量 .globl _end_ofs _end_ofs: .word _end - _start c语言:声明这个变量,然后再调用这个变量 void ...
- matlab 2017版本修改帮助文档为离线
安装matlab2017a后发现,帮助文档为在线版本,必须关联账号. 经过查询资料,帮助文档默认是使用web, on mathworks.com 可以将帮助文档改为离线版本. 具体修改方法: Pref ...