[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] ...
随机推荐
- ThinkPHP---案例2--职员管理功能
[一]准备工作 (1)创建菜单,修改跳转路径 <li> <a href="javascript:;" class="workerManage" ...
- java中的数学函数Math方法记录
1,三角函数与属性Math.sin() -- 返回数字的正弦值Math.cos() -- 返回数字的余弦值Math.tan() -- 返回数字的正切值Math.asin() -- 返回数字的反正弦值M ...
- Spring Security 介绍与Demo
一.Spring Security 介绍 Spring Security 是针对Spring项目的安全框架,也是Spring Boot底层安全模块的默认技术选型.我们仅需引入spring-boot-s ...
- 搭建私有docker仓库
安装docker yum install docker 启动docker systemctl start docker 拉取registry镜像 docker pull registry 创建仓库配置 ...
- 多校1007 Naive Operations
>>点击进入原题测试<< 思路:好像是第一次这么印象深刻的写线段树,说实话,这个题确实很有意思,值得学习. 看了大神讲解视频,但是自己写的还是超时了. 参考来自 https:/ ...
- 1031. Hello World for U
Given any string of N (>=5) characters, you are asked to form the characters into the shape of U. ...
- 使用JdbcTemplate和JdbcDaoSupport
[Spring对JDBC的支持] [JDBCTemplate简介] 1.为了是JDBC更加易于使用,Spring在JDBC API上定义了一个抽象层,以此建立一个JDBC存取框架. 2.作为Sprin ...
- JavaScript学习总结(12)——2016 年 7 个顶级 JavaScript 框架
当涉及到Web开发时,JavaScript框架往往是一些开发人员和企业最受欢迎的平台.可能,你有机会尝试过一两个顶级的JavaScript框架,但你仍然有点不确定哪个才是最佳的最值得掌握的,或者哪个值 ...
- 页面中插入视频的方法---video/embed/iframe总结
1. video标签 当前主流的方法当然是HTML5中的video标签了,但是 当前,video 元素只支持三种视频格式: Ogg = 带有 Theora 视频编码和 Vorbis 音频编码的 Ogg ...
- [luoguP3252] [JLOI2012]树(DP)
传送门 树上前缀和. 在树上找一条权值和为 s 的链,其中这个链上的点按深度递增(递减)(不同) dfs 每搜到一个点求它的前缀和 sum[x],放入 set 中. 在 set 中找 sum[x] - ...