【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,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...
随机推荐
- eclipse安装adt插件后工具栏不显示android相关图标
到eclipse官网下载luna(开发android,推荐此版本,当前最新版4.4.2)版本的eclipse http://ftp.jaist.ac.jp/pub/eclipse/technology ...
- python-GUI,生成ssn
第一次做这个, 样子有点丑,主要是实现功能,做测试的时候,经常要用到身份证号.手机号.姓名等,这里先生成ssn,后续研究怎么做成客户端 代码: from tkinter import * from u ...
- Report CodeForces - 631C (栈)
题目链接 题目大意:给定序列, 给定若干操作, 每次操作将$[1,r]$元素升序或降序排列, 求操作完序列 首先可以发现对最后结果有影响的序列$r$一定非增, 并且是升序降序交替的 可以用单调栈维护这 ...
- poj2417 Discrete Logging BSGS裸题
给a^x == b (mod c)求满足的最小正整数x, 用BSGS求,令m=ceil(sqrt(m)),x=im-j,那么a^(im)=ba^j%p;, 我们先枚举j求出所有的ba^j%p,1< ...
- Leetcode 78
//和77类似的问题,可以放在一起记忆class Solution { public: vector<vector<int>> subsets(vector<int> ...
- c#将Excel数据导入到数据库的实现代码
这篇文章主要介绍了c#将Excel数据导入到数据库的实现代码,有需要的朋友可以参考一下 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: 代码: 代码如下: using Syste ...
- POJ 3481 treap
这是利用treap写的二叉排序树,只要理解其中旋转能够改变树的左右子树平衡度,即高度之差,差不多就能掌握treap树的要领了. 相对于其他高级BST,treap树实现应该算最简单了,利用的是随机树产生 ...
- 87. Scramble String *HARD* 动态规划
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Automatic Login Using sshpass
#! /bin/bash user=root password=12345678 remote_ip=192.168.3.140 sshpass -p $password ssh $user@$rem ...
- 浅谈Obejct.assign
Object.assign属于浅拷贝 Object.assign只能拷贝:可被枚举的属性,自有属性,string或者Symbol类型是可以被直接分配的 var ab={ name:"没有被覆 ...