题意

有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的更多相关文章

  1. POJ 3169 Layout(差分约束啊)

    题目链接:http://poj.org/problem? id=3169 Description Like everyone else, cows like to stand close to the ...

  2. POJ 3169.Layout 最短路

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11612   Accepted: 5550 Descripti ...

  3. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 差分约束的解释:http://www.cnblogs.com/void/archive/2011/08/26/2153928.h ...

  4. POJ 3169 Layout (spfa+差分约束)

    题目链接:http://poj.org/problem?id=3169 题目大意:n头牛,按编号1~n从左往右排列,可以多头牛站在同一个点,给出ml行条件,每行三个数a b c表示dis[b]-dis ...

  5. POJ 3169 Layout (HDU 3592) 差分约束

    http://poj.org/problem?id=3169 http://acm.hdu.edu.cn/showproblem.php?pid=3592 题目大意: 一些母牛按序号排成一条直线.有两 ...

  6. poj 3169 Layout(差分约束+spfa)

    题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...

  7. poj 3169 Layout (差分约束)

    3169 -- Layout 继续差分约束. 这题要判起点终点是否连通,并且要判负环,所以要用到spfa. 对于ML的边,要求两者之间距离要小于给定值,于是构建(a)->(b)=c的边.同理,对 ...

  8. poj 3169 Layout 差分约束模板题

    Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6415   Accepted: 3098 Descriptio ...

  9. POJ 3169 Layout (差分约束系统)

    Layout 题目链接: Rhttp://acm.hust.edu.cn/vjudge/contest/122685#problem/S Description Like everyone else, ...

随机推荐

  1. string manipulation in game development-C # in Unity -

    ◇ string manipulation in game development-C # in Unity - It is about the various string ● defined as ...

  2. hibernate 一对一(One-to-One)

    一对一(one-to-one)实例(Person-IdCard) 一对一的关系在数据库中表示为主外关系.例如.人和身份证的关系.每个人都对应一个身份证号.我们应该两个表.一个是关于人信息的表(Pers ...

  3. [BZOJ5133][CodePlus2017年12月]白金元首与独舞

    bzoj luogu 题意 给你一个\(n*m\)的网格,每个位置上有一个箭头指向上或下或左或右.有些位置上还没有箭头,现在要求你在这些没有箭头的位置上填入箭头,使得从网格的任意一个位置开始,都可以沿 ...

  4. phpredis的使用

    phpredis的具体使用方法可以参照:https://github.com/phpredis/phpredis

  5. WPF 自定义绕圈进度条(转)

    在设计界面时,有时会遇到进度条,本次讲解如何设计自定义的绕圈进度条,直接上代码: 1.控件界面 <UserControl x:Class="ProgressBarControl&quo ...

  6. yii 操作cookie

    原文地址:http://blog.sina.com.cn/s/blog_664c9f650100yqkn.html 设置cookie: $cookie = new CHttpCookie('mycoo ...

  7. hive外表parquet文件

    外表关联parquet文件 1. 为什么关联了一次数据文件就不能二次被使用: 2. 为什么删除了employee,select还是可以而且有数据,1,2可能是一个问题   外表drop只是metada ...

  8. GX 编译器 的搭建

    1.交叉编译器 /etc/profile export JAVA_HOME=/opt/jdk1.6.0_45PATH=/opt/gx/csky-elf/bin:/opt/gx/arm-elf/bin: ...

  9. juc线程池原理(六):jdk线程池中的设计模式

    一.jdk中默认线程池中的代理模式 单例类线程池只有一个线程,无边界队列,适合cpu密集的运算.jdk中创建线程池是通过Executors类中提供的静态的方法来创建的,其中的单例类线程池的方法如下: ...

  10. 常用hash算法及评测[转]

    RS hash 算法 unsigned int RSHash(char* str, unsigned int len) {     unsigned int b    = 378551;     un ...