题目链接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 847H - Load Testing

    847H - Load Testing 思路:dp. 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...

  2. typescripts学习

    可选与默认参数 可选参数:在参数名后面,冒号前面添加一个问号,则表明该参数是可选的.如下代码: function buildName(firstName: string, lastName?: str ...

  3. ccf行车路线

    #include<bits/stdc++.h> using namespace std; const long long INF = 1e18; int n,m; struct node{ ...

  4. CentOS 7中firewall防火墙详解和配置以及切换为iptables防火墙

    官方文档介绍地址: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Gui ...

  5. Sql server函数的学习2(游标函数、日期函数、字符串操纵函数)

    一.游标函数与变量 游标可以处理多行数据,在过程循环中一次访问一行.和基于集合的高效操作相比,这个功能对系统资源的消耗更大. 可以用一个函数和两个全局变量来管理游标操作 1.CURSOR_STATUS ...

  6. 封装一个简单的原生js焦点轮播图插件

    轮播图实现的效果为,鼠标移入左右箭头会出现,可以点击切换图片,下面的小圆点会跟随,可以循环播放(为了方便理解,没有补2张图做无缝轮播).本篇文章的主要目的是分享封装插件的思路. 轮播图我一开始是写成非 ...

  7. CF 459A && 459B && 459C && 459D && 459E

    http://codeforces.com/contest/459 A题 Pashmak and Garden 化简化简水题,都告诉平行坐标轴了,数据还出了对角线,后面两个点坐标给的范围也不错 #in ...

  8. MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(三)

    MyEclipse超值折扣 限量 100 套! 立即开抢>> [MyEclipse最新版下载] MyEclipse支持Java EE技术(如JAX-WS和EJB 3.0),它们以功能包的形 ...

  9. SOD范例

    SOD申请台站波形数据范例: <?xml version="1.0"?> <sod> <eventArm> <fdsnEvent> ...

  10. ESXi6.5中将虚拟机从厚置备转换为精简置备

    本文来自:https://blog.csdn.net/wangjingkaibear/article/details/77097041 用ESXi做虚拟化,创建了一个原始虚拟机并安装好系统做好基本设置 ...