[BZOJ 3126] Photo
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=3126
[算法]
差分约束系统
注意SPFA判负环的条件应为 : 若所有点入队次数之和 > 点数 + 边数,说明有负环
[代码]
#include<bits/stdc++.h>
using namespace std;
#define MAXN 200010
const int inf = 2e9; int n,m,tot;
int head[MAXN]; struct edge
{
int to,w,nxt;
} e[MAXN << ]; template <typename T> inline void read(T &x)
{
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u,int v,int w)
{
tot++;
e[tot] = (edge){v,w,head[u]};
head[u] = tot;
}
inline int spfa(int s)
{
int l,r,cnt = ;
static int dist[MAXN];
static bool inq[MAXN];
priority_queue< int , vector< int > , greater< int > > q;
while (!q.empty()) q.pop();
for (register int i = ; i <= n; i++)
{
dist[i] = inf;
inq[i] = false;
}
q.push(s);
dist[s] = ;
inq[s] = true;
cnt = ;
while (!q.empty())
{
int u = q.top();
q.pop();
inq[u] = false;
for (register int i = head[u]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (dist[u] + w < dist[v])
{
dist[v] = dist[u] + w;
if (!inq[v])
{
cnt++;
if (cnt > * m + * n) return -;
inq[v] = true;
q.push(v);
}
}
}
}
return dist[n] == inf ? - : dist[n];
} int main()
{ read(n); read(m);
for (register int i = ; i <= m; i++)
{
int a,b;
read(a); read(b);
if (a > b) swap(a,b);
addedge(a - ,b,);
addedge(b,a - ,-);
}
for (register int i = ; i <= n; i++)
{
addedge(i,i - ,);
addedge(i - ,i,);
}
printf("%d\n",spfa()); return ;
}
[BZOJ 3126] Photo的更多相关文章
- 数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo
3126: [Usaco2013 Open]Photo Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 222 Solved: 116 Descrip ...
- Bzoj 3126[Usaco2013 Open]Photo 题解
3126: [Usaco2013 Open]Photo Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 335 Solved: 169[Submit] ...
- ●BZOJ 3126 [Usaco2013 Open]Photo
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3126 题解: 单调队列优化DP,神奇.. (好像某次考试考过,当时我用了差分约束+SPFA优 ...
- bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp
Description 给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点 Input * Line 1: Two integers N and M. * Lines 2..M+ ...
- BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)
洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...
- bzoj 3126 单调队列优化dp
能转移的最左是其左边完整区间的最右左端点,最右是能覆盖它的最左左端点-1 #pragma GCC optimize ("O3") #include<cstdio> #i ...
- bzoj usaco 金组水题题解(2)
续.....TAT这回不到50题编辑器就崩了.. 这里塞40道吧= = bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 比较经典的最小割?..然而 ...
- USACO 刷题记录bzoj
bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草——背包 #include<cstdio> #include<cstring> #incl ...
- bzoj3126[Usaco2013 Open]Photo 单调队列优化dp
3126: [Usaco2013 Open]Photo Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 374 Solved: 188[Submit] ...
随机推荐
- MxCAD5.2 20181022更新
下载地址: http://www.mxdraw.com/ndetail_10108.html 1. 开放VIP功能,无需购买即可使用 2. 修正一些图纸打开和保存出错的问题 3. 修改填充命令,对某些 ...
- C# Task详解
1.Task的优势 ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便.比如: ◆ ThreadPool不支持线程的取消.完成.失败通知等交互性 ...
- [Luogu] P4366 [Code+#4]最短路
题目背景 在北纬 91° ,有一个神奇的国度,叫做企鹅国.这里的企鹅也有自己发达的文明,称为企鹅文明.因为企鹅只有黑白两种颜色,所以他们的数学也是以二进制为基础发展的. 比如早在 1110100111 ...
- linux nethogs-终端下的网络流量监控工具
推荐:更多linux 性能监测与优化 关注:linux命令大全 有很多适用于Linux系统的开源网络监视工具.比如说,你可以用命令iftop来检查带宽使用情况.netstat用来查看接口统计报告,还有 ...
- 浅谈微信小程序对于房地产行业的影响
前几日,我们曾经整理过一篇文章是关于微信小程序对于在线旅游业的影响的一些反思(浅谈微信小程序对OTA在线旅游市场的影响),近日由于生活工作的需要走访了一些房地产的住宅商品房,突然想到微信小程序对于房地 ...
- Python初学者容易忽略的一些细节
1.Python中当让给变量1赋值上变量2的值时,变量1直接指向值在内存中存储的地址,即变量1存储的是变量2的值而不是指向变量 2."""的用法 1.多行注释 2.多行输 ...
- springcloud(十三):Ribbon客户端负载均衡实例
一.采用默认的负载均衡策略:RoundRobinRule 轮询策略 1.修改提供者原的控制类 在之前的eureka-client-provider项目的CenterController.java中加入 ...
- POJ 1811 大整数素数判断 Miller_Rabin
#include <cstdio> #include <cstring> #include <cmath> #include <ctime> #incl ...
- python函数基础:嵌套函数、作用域、匿名函数、高阶函数、递归函数
嵌套函数: 1. 函数内部可以再定义函数 2. 函数只有被调用之后才会执行 看如下代码: age = 18 def func1(): age = 22 print(age) def func2(): ...
- 史上超全面的Neo4j使用指南
Create by yster@foxmail.com 2018-7-10 我的博客:https://blog.csdn.net/yueshutong123 W3Cschool文档:https://w ...