POJ3169:Layout(差分约束)
Layout
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15705 | Accepted: 7551 |
题目链接:http://poj.org/problem?id=3169
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.
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
Sample Output:
27
Hint:
Explanation of the sample:
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.
The best layout, in terms of coordinates on a number line, is to put cow #1 at 0, cow #2 at 7, cow #3 at 10, and cow #4 at 27.
题意:
有n只牛,之后给出m个关系x,y,z满足x号牛和y号牛相距不超过z,之后还会有k个关系x,y,z满足x,y相距至少为z。
现在问1号牛和n号牛最大的距离可能是多少,如果此最大值不存在,输出-1;如若这个最大值有无穷多个,则输出-2。
题解:
就是个差分约束模板题,建个图跑一跑就好了。注意一下最后输出的顺序。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#define INF 9999999999999999
using namespace std;
typedef long long ll;
const int N = ,M = ;
ll d[N];
int vis[N],head[N],c[N];
int n,ml,md;
struct Edge{
int u,v,w,next;
}e[M<<];
int tot;
void adde(int u,int v,int w){
e[tot].u=u;e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
ll spfa(int s){
queue <int> q;
for(int i=;i<=n;i++) d[i]=INF;
q.push(s);vis[s]=;d[]=;c[]=;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
if(c[u]>n) return -;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(d[v]>=d[u]+e[i].w){
d[v]=d[u]+e[i].w;
if(!vis[v]){
vis[v]=;
q.push(v);
c[v]++;
}
}
}
}
return d[n];
}
int main(){
cin>>n>>ml>>md;
memset(head,-,sizeof(head));
for(int i=;i<=ml;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);
}
for(int i=;i<=md;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
adde(v,u,-w);
}
ll flag = spfa();
if(flag==INF){
cout<<-;
return ;
}
else if(flag==-) cout<<-;
else cout<<d[n];
return ;
}
POJ3169:Layout(差分约束)的更多相关文章
- POJ-3169 Layout (差分约束+SPFA)
POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...
- POJ3169:Layout(差分约束)
http://poj.org/problem?id=3169 题意: 一堆牛在一条直线上按编号站队,在同一位置可以有多头牛并列站在一起,但编号小的牛所占的位置不能超过编号大的牛所占的位置,这里用d[i ...
- POJ3169 Layout(差分约束系统)
POJ3169 Layout 题意: n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛 ...
- POJ 3169 Layout(差分约束+链式前向星+SPFA)
描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- POJ 3169 Layout (差分约束)
题意:给定一些母牛,要求一个排列,有的母牛距离不能超过w,有的距离不能小于w,问你第一个和第n个最远距离是多少. 析:以前只是听说过个算法,从来没用过,差分约束. 对于第 i 个母牛和第 i+1 个, ...
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- POJ 3169 Layout(差分约束 线性差分约束)
题意: 有N头牛, 有以下关系: (1)A牛与B牛相距不能大于k (2)A牛与B牛相距不能小于k (3)第i+1头牛必须在第i头牛前面 给出若干对关系(1),(2) 求出第N头牛与第一头牛的最长可能距 ...
- Bellman-Ford算法:POJ No.3169 Layout 差分约束
#define _CRT_SECURE_NO_WARNINGS /* 4 2 1 1 3 10 2 4 20 2 3 3 */ #include <iostream> #include & ...
随机推荐
- 003---socket介绍
socket介绍 什么是socket? socket是应用层与tcp/ip协议族通信的中间软件抽象层,它是一组接口.在设计模式中.其实就是一个门面模式.我们无需深入理解tcp/udp协议,socket ...
- R语言学习笔记(十一):零碎知识点(26-30)
26--aggregate( ) 函数aggregate()对分组中的每一个变量调用tapply()函数. aggregate(a,list,f) 第二个参数必须是列表.也就是因子部分. 第三个参数即 ...
- Javaweb——四则运算---18.11.01
---恢复内容开始--- test.jsp <%@ page language="java" contentType="text/html; charset=utf ...
- HTML5 + JS 调取摄像头拍照下载
<video id="video" width="640" height="480" autoplay></video&g ...
- 在WPF中自定义控件(3) CustomControl (下)
原文:在WPF中自定义控件(3) CustomControl (下) 在WPF中自定义控件(3) CustomControl (下) ...
- 子串查询(二维前缀数组) 2018"百度之星"程序设计大赛 - 资格赛
子串查询 Time Limit: 3500/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Subm ...
- python发起请求提示UnicodeEncodeError
具体错误: UnicodeEncodeError: 'latin-1' codec can't encode characters in position 73-74: Body ('测试') is ...
- 【Java】Map转换器
描述: 在控制层接收参数时候, 往往会出现Json格式需要转换为Bean. 通常一两个字段可以用new去save pojo, 但字段多的情况呢? 以下就是为了解决这个尴尬情况, 自己写一个转换工具类 ...
- (vue01)vue环境搭建
腾讯,百度,网易....这么大媒体平台咋老推送这么lower的信息? 你们不缺钱啊....我这么善良的孩子都别你们带坏了 强烈鄙视马化腾 强烈鄙视李彦宏 参考地址: https://segmentfa ...
- 09-Mysql数据库----外键的变种
本节重点: 如何找出两张表之间的关系 表的三种关系 一.介绍 因为有foreign key的约束,使得两张表形成了三种了关系: 多对一 多对多 一对一 二.重点理解如果找出两张表之间的关系 分析步骤: ...