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 & ...
 
随机推荐
- 阿里云异常网络连接-可疑WebShell通信行为的分析解决办法
			
2018年10月27日接到新客户网站服务器被上传了webshell脚本木马后门问题的求助,对此我们sine安全公司针对此阿里云提示的安全问题进行了详细分析,ECS服务器被阿里云提示异常网络连接-可疑W ...
 - Hive 函数之内置运算符
			
本章介绍Hive的内置运算符.在Hive有四种类型的运算符: 关系运算符 算术运算符 逻辑运算符 复杂运算符 关系运算符 这些操作符被用来比较两个操作数.下表描述了在Hive中可用的关系运算符: 运算 ...
 - Jmeter从文件中读取参数值
			
1. 通过函数助手,从本地文件中取值选项->函数助手对话框->选择__CSVRead函数->调用参数其中,函数助手对话框中,第一栏填写本地文件所在地址,第二栏写需要入参的值,有点类似 ...
 - Python 中的容器 collections
			
写在之前 我们都知道 Python 中内置了许多标准的数据结构,比如列表,元组,字典等.与此同时标准库还提供了一些额外的数据结构,我们可以基于它们创建所需的新数据结构. Python 附带了一个「容器 ...
 - [USACO19JAN]Cow Poetry
			
题面 Solution: 这是一道很好的dp题. 一开始看不懂题面没有一点思路,看了好久题解才看懂题目... \(y[i]\) 为第 \(i\) 个词结尾,\(l[i]\) 为第 \(i\) 个词长度 ...
 - 输出1-n的全排(递归C++)
			
[问题描述] 输出1到n之间所有不重复的排列,即1到n的全排,要求所产生的任一数列不含有重复的数字. [代码展示] #include<iostream>using namespace st ...
 - 无缘无故出现npm 解析异常的的问题 解决方案
			
npm cache clean --force try if false delete package.lock.json try again if false npm set registry ht ...
 - [leetcode-646-Maximum Length of Pair Chain]
			
You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...
 - redis基础和通用key操作
			
redis是什么? redis开源的,构建于内存的数据结构的nosql数据库.常被用于数据存储,缓存处理和消息处理. redis的优势? 1.极高的读写能力 2.丰富的数据类型 3.原子性操作 4.支 ...
 - Python模块学习:logging 日志记录
			
原文出处: DarkBull 许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net ...