题目链接http://poj.org/problem?id=3169

题目大意:

一些牛按序号排成一条直线。

有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离。如果没有输出-1,如果可以随便排输出-2,否则输出最大的距离。

首先关于差分约束:https://blog.csdn.net/consciousman/article/details/53812818

了解了差分约束之后就知道该题典型的差分约束+spfa即可。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
struct pot{
int to;
int next;
int len;
}edge[];
int next[];
int d[];
int cnt[];
bool vis[];
int que[];
int tot=,front=,back=;
int n,m,q;
void add(int x,int y,int t)
{
edge[tot].to=y;
edge[tot].len=t;
edge[tot].next=next[x];
next[x]=tot++;
}
bool spfa()
{
d[]=;
vis[]=true;
que[front++]=;
cnt[]++;
while(front!=back)
{
back++;
if(back>=)back=;
vis[que[back]]=false;
for(int i = next[que[back]];i!=-;i=edge[i].next)
{
if(d[edge[i].to]>d[que[back]]+edge[i].len)
{
d[edge[i].to]=d[que[back]]+edge[i].len;
if(!vis[edge[i].to])
{
que[front++]=edge[i].to;
if(front>=)front=;
vis[edge[i].to]=true;
cnt[edge[i].to]++;
if(cnt[edge[i].to]>n)return false;
}
}
}
}
return true;
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i = ;i <= n ; i++)
{
next[i]=-;
d[i]=INF;
vis[i]=false;
cnt[i]=;
}
for(int i = ; i < m ; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
int t=a;
a=b;
b=t;
}
add(a,b,c);
}
for(int i = ; i < q ; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(b>a)
{
int t=a;
a=b;
b=t;
}
add(a,b,-c);
}
if(!spfa())printf("-1\n");
else if(d[n]==INF)printf("-2\n");
else printf("%d\n",d[n]);
return ;
}

【poj3169】【差分约束+spfa】的更多相关文章

  1. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  2. POJ-3169 Layout (差分约束+SPFA)

    POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...

  3. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  4. 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...

  5. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  6. poj Layout 差分约束+SPFA

    题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...

  7. BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)

    BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...

  8. POJ-3159.Candies.(差分约束 + Spfa)

    Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 40407   Accepted: 11367 Descri ...

  9. 图论分支-差分约束-SPFA系统

    据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...

随机推荐

  1. Codeforces 96C - Hockey

    96C - Hockey 字符串处理 代码: #include<bits/stdc++.h> using namespace std; #define ll long long ; con ...

  2. ISO 8859-1 对照表 (扩展ASCII码表)

    1. 0---127 是ASCII码 2.128--255 加了一些特殊符号 DEC OCT HEX BIN Symbol HTML Number HTML Name Description 128 ...

  3. 肠道型(enterotype)简介

    An enterotype is a classification of living organisms based on its bacteriological ecosystem in the ...

  4. Java基础-this关键字和构造方法(10)

    this关键字 方法被哪个对象调用,this就代表那个对象当局部变量隐藏成员变量时,使用this关键字(例如构造方法和访问器). 构造方法 构造方法作用概述 给对象的数据进行初始化 构造方法格式 方法 ...

  5. python-day68--模型层基础(model)

    一.ORM : object relationship mapping   对象映射关系 映射关系: 表名 <-------> 类名 字段 <-------> 属性 表记录 & ...

  6. gleez开发环境搭建

    一.虚拟主机目录配置 1.配置apache服务器 Apache是常用的web服务器,即常见的用来处理http协议,处理网页的. Apache的配置文件都存放在/etc/apache2/目录,这里有很多 ...

  7. 异常检测——局部异常因子(Local Outlier Factor ,LOF)算法

    在中等高维数据集上执行异常值检测的另一种有效方法是使用局部异常因子(Local Outlier Factor ,LOF)算法.1.算法思想 LOF通过计算一个数值score来反映一个样本的异常程度.这 ...

  8. 无名管道跟dup,dup的使用

    参考资料: http://www.tldp.org/LDP/lpg/node11.html http://blog.csdn.net/yeyuangen/article/details/6852682 ...

  9. Css的向左浮动、先右浮动、绝对定位、相对定位的简单使用

    1.div层的浮动 1)div向左浮动.向右浮动 <!doctype html> <html> <head> <meta charset="utf- ...

  10. 我的一起开源网 www.17ky.net上线了

    .net开源生态的落后,使得.net开发人员所拥有的开源资源比其他语言的开发者少了很多,这也使得笔者很早之前就喜欢收集各种开源项目,经常会去逛codeplex,开源中国社区等网站,同时也喜欢在自己或公 ...