Description

当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些。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

4 2 1
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

27

四只牛分别在0,7,10,27.

——————————————————————————————
这道题是裸的差分约束 属: 最大距离——最短路
记第i头牛的位置为di 因为编号小的不能大于编号大的
即di>=di-1
这个时候 i向i-1连一条权值为0的边 保证位置关系
如果i j 有好感 那么d[i]<=d[j]+L 这个时候就从j向i连一条长度为L的边
如果 i j 相互厌恶 那么d[j]<=d[i]-d 这个时候就从i向j连一条长度为-d的边
这样连边之后跑一遍最短路就可以了 
至于为什么是最短路 因为我们这样连边都是连的最大的边了
最短路最后得到的是d[n]-d[1]<=a,最长路最后得到的是d[n]-d[1]>=b
因为要求d[n]-d[1]的最大值,所以要跑最短路
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define LL long long
const int M=1e3+,N=1e5+,inf=0x3f3f3f3f;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
int n,ml,md,h[M],vis[M];
int x,y,w,dis[M];
int first[M],cnt;
struct node{int to,next,w;}e[N];
void ins(int a,int b,int w){e[++cnt]=(node){b,first[a],w}; first[a]=cnt;}
std::queue<int>q;
bool f=false;
void spfa(){
memset(dis,0x3f,sizeof(dis));
q.push(); dis[]=;
vis[]=; h[]=;
while(!q.empty()){
int x=q.front(); q.pop();
for(int i=first[x];i;i=e[i].next){
int now=e[i].to;
if(dis[now]>dis[x]+e[i].w){
dis[now]=dis[x]+e[i].w;
if(!vis[now]){
vis[now]=; q.push(now);
h[now]++; if(h[now]==n){f=true; return ;}
}
}
}
vis[x]=;
}
}
int main(){
n=read(); ml=read(); md=read();
for(int i=;i<=ml;i++) x=read(),y=read(),w=read(),ins(x,y,w);
for(int i=;i<=md;i++) x=read(),y=read(),w=read(),ins(y,x,-w);
for(int i=;i<n;i++) ins(i+,i,);
spfa();
if(f) printf("-1\n");
else if(dis[n]==inf) printf("-2\n");
else printf("%d\n",dis[n]);
return ;
}

bzoj 1731: [Usaco2005 dec]Layout 排队布局 ——差分约束的更多相关文章

  1. bzoj 1731 [Usaco2005 dec]Layout 排队布局——差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 对差分约束理解更深.还发现美妙博客:http://www.cppblog.com/me ...

  2. 【BZOJ1731】[Usaco2005 dec]Layout 排队布局 差分约束

    [BZOJ1731][Usaco2005 dec]Layout 排队布局 Description Like everyone else, cows like to stand close to the ...

  3. bzoj 1731: [Usaco2005 dec]Layout 排队布局【差分约束】

    差分约束裸题,用了比较蠢的方法,先dfs_spfa判负环,再bfs_spfa跑最短路 注意到"奶牛排在队伍中的顺序和它们的编号是相同的",所以\( d_i-d_{i-1}>= ...

  4. BZOJ 1731: [Usaco2005 dec]Layout 排队布局

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  5. [Usaco2005 dec]Layout 排队布局 差分约束

    填坑- 差分约束一般是搞一个不等式组,求xn-x1的最大最小值什么的,求最大值就转化成xa<=xb+w这样的,然后建图跑最短路(这才是最终约束的),举个例子 x1<=x0+2x2<= ...

  6. 1731: [Usaco2005 dec]Layout 排队布局*

    1731: [Usaco2005 dec]Layout 排队布局 题意: n头奶牛在数轴上,不同奶牛可以在同个位置处,编号小的奶牛必须在前面.m条关系,一种是两头奶牛距离必须超过d,一种是两头奶牛距离 ...

  7. 【BZOJ】1731: [Usaco2005 dec]Layout 排队布局

    [题意]给定按编号顺序站成一排的牛,给定一些约束条件如两牛距离不小于或不大于某个值,求1和n的最大距离.无解输出-1,无穷解输出-2. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...

  8. bzoj 1731 Layout 排队布局 —— 差分约束

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 差分约束: ML: dis[y] - dis[x] <= k,即 x 向 y 连 ...

  9. BZOJ1731:[USACO]Layout 排队布局(差分约束)

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

随机推荐

  1. ACM 第七天

    水题 B - Minimum’s Revenge There is a graph of n vertices which are indexed from 1 to n. For any pair ...

  2. 【week2】 四则运算改进

    四则运算满足简单加减乘除,以及包含括号的复杂四则运算. 代码描述: 1.采用random随机数产生要参与计算的数字,以及运算符号 2.采用Scanner获取控制台输入的结果,与计算出来的结果进行比对, ...

  3. mac mysql连接报错ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    找了半天 又是kill进程,又是改设置文件,又是重启电脑,都不管用 翻到stackoverflow上的解决方案,实施成功: 原文链接:https://stackoverflow.com/questio ...

  4. 火狐浏览器(FireFox)安装Flash插件失败处理方法

    最近不知道怎么了,总是嫌弃IE,可能是被网络流量监测的网址给搞得了,弄了火狐浏览器,也安装了猎豹,这里不对浏览器做评价 好多朋友安装好火狐(FireFox)的时候发现之前不是有装IE的Flash播放插 ...

  5. shit antd & Merry Christmas bug

    shit antd & Merry Christmas bug https://github.com/ant-design/ant-design/issues/13098 antd 玩大了? ...

  6. Linux中实现在系统启动时自动加载模块

    下面是以前学习Linux时写的,后来仔细研究rc.sysinit后发现,只需要修改下列地方就可以了,不必这么麻烦的: rc.sysinit中有这样的一段代码: # Load other user-de ...

  7. bootstrap 有些控件需要调用锚点,会与angular 路由 冲突

    最简单的方法 就是 在 #号前加/, 但有人说 在服务器上回失效,也不知道是什么原理.慎用 最靠谱的方法 就 是 使用bootstrap中的js控制控件, 比如轮播图的上一页 下一页,就可以在 ang ...

  8. hdu2121-Ice_cream’s world II

    给出一个有向图,求最小树形图和它的最小的根. 分析 这个题又写了一晚上-我之前的朱刘算法写法是我乱想的,只有那道题可以过--所以去找了一份代码来看,发现我的写法超级麻烦啊,所以就学习了一下那种写法,非 ...

  9. Java中的缓冲流详解

    缓冲流增强了读写文件的能力,比如Student.txt是一个学生的名单,每个姓名占一行.如果我们想要读取名字,那么每次必须读取一行,使用FileReader流很难完成这样的任务,因为我们不清楚一行有多 ...

  10. html的body内标签之input系列1

    1. Form的作用:提交当前的表单. 类似于去了银行提交的纸质单子,递到后台去办理相关业务. text,password只有输入的功能:button,submit只有点击的功能.想要把这些信息提交, ...