POJ 3169 C - Layout
题意
有n头奶牛从1到n编号,按照编号顺序站成一排,有可能有多头奶牛站在同一个坐标上。一些奶牛互相喜欢,所以他们的距离不能大于某个距离,一些奶牛互相讨厌,所以他们的距离不能小于某个距离,请计算如果可能的话1到n的最大距离是多少
分析
标准的差分约束的裸题。题中的条件可以做如下转化
1.A和B的距离不得超过x: B-A<=x
2.C和D的距离不得小于y:C-D>=y也就是D-C<=-y
3.奶牛按照编号顺序排序:位置 pos[i+1]-pos[i]>=0也就是pos[i]-pos[i+1]<=0
这样把关系全部变成了小于关系,求最大距离的话建图以后跑一个最短路就行。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue> using namespace std;
const int maxn=+;
const int maxm=+;
const int INF=; int head[maxn],Next[maxm],to[maxm],val[maxm];
int n,ml,md,sz;
void add_edge(int a,int b,int w){
++sz;
to[sz]=b;
val[sz]=w;
Next[sz]=head[a];
head[a]=sz;
}
int a,b,w;
int spfa(){
int d[maxn],vis[maxn],cnt[maxn];
memset(vis,,sizeof(d));
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++)d[i]=INF;
queue<int>q;
q.push(n);
d[n]=;vis[n]=;cnt[n]=;
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
// cout<<u<<endl;
for(int i=head[u];i;i=Next[i]){
int v=to[i];
if(d[v]>d[u]+val[i]){
d[v]=d[u]+val[i];
if(!vis[v]){
vis[v]=;
q.push(v);
cnt[v]++;
if(cnt[v]>n){
return -;
}
}
}
}
}
if(d[]==INF)
return -;
return d[];
}
int main(){
scanf("%d%d%d",&n,&ml,&md);
for(int i=;i<=ml;i++){
scanf("%d%d%d",&a,&b,&w);
add_edge(b,a,w);
}
for(int i=;i<=md;i++){
scanf("%d%d%d",&a,&b,&w);
add_edge(a,b,-w);
}
for(int i=;i<n;i++){
add_edge(i,i+,);
}
int ans=spfa();
printf("%d",ans); return ;
}
POJ 3169 C - Layout的更多相关文章
- POJ 3169 Layout(差分约束啊)
题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...
- POJ 3169.Layout 最短路
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11612 Accepted: 5550 Descripti ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...
- POJ 3169 Layout (spfa+差分约束)
题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...
- POJ 3169 Layout (HDU 3592) 差分约束
http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...
- poj 3169 Layout(差分约束+spfa)
题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...
- poj 3169 Layout (差分约束)
3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...
- poj 3169 Layout 差分约束模板题
Layout Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6415 Accepted: 3098 Descriptio ...
- POJ 3169 Layout (差分约束系统)
Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...
随机推荐
- 转:C++模板特化的概念
http://blog.csdn.net/yesterday_record/article/details/7304025 很久没有看C++,在看STL源码剖析时,看到一个function templ ...
- 《Ubuntu入门基础》第三篇
创建虚拟磁盘
- JavaScript例子
模态框(JavaScript) <!DOCTYPE html> <html lang="en"> <head> <meta charset ...
- Ubuntu下安装为知笔记
之前在Windows下用的是有道云笔记,但是后来开始习惯使用Linux开发,有道云官方并没有提供Ubuntu的版本,所以权衡之下,选择了为知笔记,安装步骤: sudo add-apt-reposito ...
- Python之operator库
operator库常用方法 Function Syntax add(a+b) a + b concat(seq1,seq2) seq1 + seq2 contains(seq, obj) obj in ...
- CentOS上面搭建SVN服务器
1.安装svn sudo yum install subversion 查看安装位置 which svnserve 确认安装成功 svnserve --version 2.修改全局配置文件修改全局配置 ...
- 关于Android Studio上得处女座福音功能——reformat code
在mac上,选中需要的代码,然后 Option+(shift) + Command + L 全部重新排列!!爽飞!
- java中String和char的区别
首先来看一下Java的数据类型.Java 包括两种数据类型: 1.原始数据类型(primitive data type):byte,short, char, int, long,float,doubl ...
- 安装S_S相关报错的troubleshooting
在安装S_S server时,在Debian上会出现类似如下的报错: File , in <module> sys.exit(main()) File , in main config = ...
- git的操作
学习源头: https://www.cnblogs.com/yaoxc/p/3946280.html https://www.cnblogs.com/jeremylee/p/5715289.html ...