Codevs 1242 布局 2005年USACO(差分约束)
1242 布局 2005年USACO
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题目描述 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 Description
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 Description
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
样例输出 Sample Output
27
数据范围及提示 Data Size & Hint
分类标签 Tags
最短路 图论 USACO 2005年
/*
差分约束.
由约束条件可得
(1)dis[y1]-dis[x1]<=T.
(2)dis[x2]-dis[y2]<=-D.
(3)dis[i]-dis[i+1]>=0.
建图后spfa跑最短路.
*/
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#define MAXN 500001
using namespace std;
struct data{int v,next,x;}e[MAXN*3];
int n,m,dis[MAXN],head[MAXN],cut,k,c[MAXN];
bool b[MAXN];
int read()
{
int x=0;char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x;
}
void add(int u,int v,int z)
{
e[++cut].v=v;
e[cut].x=z;
e[cut].next=head[u];
head[u]=cut;
}
int spfa()
{
queue<int>q;q.push(1);
memset(dis,127/3,sizeof(dis));
dis[1]=0;
while(!q.empty())
{
int u=q.front();q.pop();b[u]=false;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(dis[v]>dis[u]+e[i].x)
{
dis[v]=dis[u]+e[i].x;
if(!b[v])
{
b[v]=true,q.push(v);c[v]++;
if(c[v]>=n) return -1;;
}
}
}
}
if(dis[n]==dis[0]) return -2;
return dis[n];
}
int main()
{
int x,y,z;
n=read(),m=read(),k=read();
for(int i=1;i<=m;i++)
{
x=read(),y=read(),z=read();
add(x,y,z);
}
for(int i=1;i<=k;i++)
{
x=read(),y=read(),z=read();
add(y,x,-z);
}
for(int i=1;i<=n;i++) add(i+1,i,0);
printf("%d",spfa());
return 0;
}
Codevs 1242 布局 2005年USACO(差分约束)的更多相关文章
- codevs 1242 布局(查分约束+SPFA)
/* 查分约束. 给出的约束既有>= 又有<= 这时统一化成一种 Sb-Sa>=x 建边 a到b 权值为x Sb-Sa<=y => Sa-Sb>=-y 建边 b到a ...
- BZOJ1731:[USACO]Layout 排队布局(差分约束)
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- codevs 1183 泥泞的道路 (二分+SPFA+差分约束)
/* 二分答案(注意精度) 对于每一个答案 有(s1+s2+s3...)/(t1+t2+t3...)>=ans 时符合条件 这时ans有变大的空间 对于上述不等式如果枚举每一条路显得太暴力 化简 ...
- [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 排队布局——差分约束
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 对差分约束理解更深.还发现美妙博客:http://www.cppblog.com/me ...
- bzoj 1731 Layout 排队布局 —— 差分约束
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1731 差分约束: ML: dis[y] - dis[x] <= k,即 x 向 y 连 ...
- bzoj 1731: [Usaco2005 dec]Layout 排队布局【差分约束】
差分约束裸题,用了比较蠢的方法,先dfs_spfa判负环,再bfs_spfa跑最短路 注意到"奶牛排在队伍中的顺序和它们的编号是相同的",所以\( d_i-d_{i-1}>= ...
- 布局(codevs 1242)
题目描述 Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们 ...
随机推荐
- 『Python基础』第2节: Python简介及入门
一. Python介绍 Python是一门高级计算机程序设计语言,1989年,荷兰的Guido von Rossum创造了它.Guido是是一个牛人,1982年,他从阿姆斯特丹大学获得了数学和计算机硕 ...
- harbor上传镜像
在harbor服务器 1. 下载测试上传使用的镜像docker pull hello-world2. 打tagdocker tag docker.io/hello-world:latest 172.1 ...
- Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form ResumeForm needs updating.
django 报错 django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fiel ...
- logback日志详细解析
1.为什么使用logback 内核重写.测试充分.初始化内存加载更小,这一切让logback性能和log4j相比有诸多倍的提升 logback非常自然地直接实现了slf4j,方便理解 支持自动去除旧的 ...
- 【web】使用ionic搭建移动端项目 icon-radio 标签在ios下全部选中的问题
这块css 导致的问题 .disable-pointer-events { pointer-events: none; }
- python运行报错:cannot import name 'InteractiveConsole'
ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' ImportError: cannot import name ...
- django-两种方式对单表的操作
单表操作的内容 我们这里对数据库单表的操作包含增删改查四部分 具体链接数据库的方式我们是通过pymysql,当然你也可以用其他的. 两种方式的概念与区别 1.新url的方式 主要就是我们每一次向后台提 ...
- react中key值的理解
react利用key来识别组件,它是一种身份标识标识,相同的key react认为是同一个组件,这样后续相同的key对应组件都不会被创建有了key属性后,就可以与组件建立了一种对应关系,react根据 ...
- CentOS7使用yum安装PostgreSQL和PostGIS
更新yum源 CentOS7默认yum源的PostgreSQL版本过低,不适合在本版本上使用.在https://yum.postgresql.org/repopackages.php上找到适合Cent ...
- Oracle数据库默认的data pump dir在哪
转自:https://zhidao.baidu.com/question/921271686131558779.html 使用select * from dba_directories;可以查到,例如 ...