【poj3169】【差分约束+spfa】
题目链接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】的更多相关文章
- O - Layout(差分约束 + spfa)
O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...
- POJ-3169 Layout (差分约束+SPFA)
POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...
- (简单) POJ 3169 Layout,差分约束+SPFA。
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- poj Layout 差分约束+SPFA
题目链接:http://poj.org/problem?id=3169 很好的差分约束入门题目,自己刚看时学呢 代码: #include<iostream> #include<cst ...
- BZOJ.4500.矩阵(差分约束 SPFA判负环 / 带权并查集)
BZOJ 差分约束: 我是谁,差分约束是啥,这是哪 太真实了= = 插个广告:这里有差分约束详解. 记\(r_i\)为第\(i\)行整体加了多少的权值,\(c_i\)为第\(i\)列整体加了多少权值, ...
- POJ-3159.Candies.(差分约束 + Spfa)
Candies Time Limit: 1500MS Memory Limit: 131072K Total Submissions: 40407 Accepted: 11367 Descri ...
- 图论分支-差分约束-SPFA系统
据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...
随机推荐
- python 函数参数介绍
python 函数参数介绍 python 使用过程总,总会遇到 *args,**kw形式的参数,总是一头雾水,而且网上介绍的或是叫法不一,为此专门深入实践进而了解了函数参数的使用 具体请看代码 #-* ...
- javascript之非构造函数的继承
这个系列的第一部分介绍了"封装",第二部分介绍了使用构造函数实现"继承". 今天是最后一个部分,介绍不使用构造函数实现"继承". 一.什么是 ...
- 27 网络通信协议 tcp udp subprocess
1.模块subprocess import subprocess cmd_str = input('请输入指令>>>') sub_obj = subprocess.Popen( cm ...
- python-day6---运算符
#了解部分#字符串+,*#列表:+,*# l1=[1,2,3]# l2=[4,5]## print(l1+l2)# print(l1*3) #比较运算符# num1=3# num2=1 # print ...
- python 爬虫之为什么使用opener对象以及为什么要创建全局默认的opener对象
基本的urlopen()函数不支持验证.cookie或其他HTTP高级功能.要支持这些功能,必须使用build_opener()函数来创建自己的自定义Opener对象. install_opener( ...
- 使用API更新供应商名称及曾用名
原文地址 更新供应商名称 EBS R12 (12.1.3) 更新供应商名称或替代供应商名称不能直接使用 pos_vendor_pub_pkg.update_vendor(p_vendor_rec =& ...
- iOS UI-Lable标签、NStimer定时器和RunLoop超级死循环
// 标签UILable -显示文字 // 1.创建标签 UILabel *lable = [[UILabel alloc] init]; // 2.设置标签的坐标和大小 [lable setFram ...
- 一篇关于oracle psu的文章(转)
Oracle Database PSU/CPU Posted on 2011-07-28 16:27 dbblog 阅读(2569) 评论(0) 编辑 收藏 1. 什么是PSU/CPU?CPU: Cr ...
- PHP生成GIF动态图片验证码
<?php /** * 调用示例 * */ session_start(); $randCode = ''; //验证码随机 $str="abcdefghjkmnpqrstuvwsyz ...
- 网络协议栈学习(一)socket通信实例
网络协议栈学习(一)socket通信实例 该实例摘自<linux网络编程>(宋敬彬,孙海滨等著). 例子分为服务器端和客户端,客户端连接服务器后从标准输入读取输入的字符串,发送给服务器:服 ...