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 & ...
随机推荐
- n点游戏
n点游戏 24点游戏是非常经典而简单的小游戏,从一堆扑克牌中抽取4张,向其中添加运算符号并使其运行结果恰等于24,这叫作24点游戏. 现在我们不再是组合24,而是组合出给定的数字n,但要求只可以利用任 ...
- Linux mysql启动与关闭
service mysql stop service mysqld start
- windows 安装 .net core 环境
windows 安装 环境说明 window10系统 .net core 1.0.1 visual studio code 安装 .net core Windows系统下安装软件基本上属于傻瓜式安装, ...
- 通过修改Host文件解决主机头访问网站的问题
网站打包发布后,一般都是通过IP地址来进行访问,但是这样不方便记忆.如何设置一个简单的域名,然后通过域名来进行访问呢?一个可行的方法就是修改本机的host文件,添加一条映射关系,把这 ...
- sed 集合(项目中的笔记)
奇数行和偶数行合并为一行: Like: Sequence number: 5398Sequence name: Glyma.16G123500.1Sequence number: 5399Sequen ...
- 使用Vue-cli 3.x搭建Vue项目
一.Vue-cli 3.x安装 Node 版本要求:Vue CLI 需要 Node.js 8.9 或更高版本 (推荐 8.11.0+) npm install -g @vue/cli 查版本是否正确 ...
- 接口测试工具postman(四)导入导出文件
1.导入json文件 2.单个文件夹导出,文件格式是 json文件 3.所有数据导出,文件格式是 json文件
- 第十篇 Python的字符串格式化
字符串格式化:就是按照你的意愿做一个拼接的过程. 1. 字符串格式化的第一种方式:百分号方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. %[ ...
- (原创)BFS广度优先算法,看完这篇就够了
BFS算法 上一篇文章讲解了DFS深度优先遍历的算法,我们说 DFS 顾名思义DEEPTH FIRET,以深度为第一标准来查找,以不撞南墙不回头的态度来发掘每一个点,这个算法思想get到了其实蛮简单. ...
- 并查集——hdu1232(入门)
传送门:畅通工程 实质是求连通分支的数量 #include <iostream> #include <cstdio> #include <algorithm> us ...