[题目链接]

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的更多相关文章

  1. 数据结构(线段树):BZOJ 3126: [Usaco2013 Open]Photo

    3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 222  Solved: 116 Descrip ...

  2. Bzoj 3126[Usaco2013 Open]Photo 题解

    3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 335  Solved: 169[Submit] ...

  3. ●BZOJ 3126 [Usaco2013 Open]Photo

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3126 题解: 单调队列优化DP,神奇.. (好像某次考试考过,当时我用了差分约束+SPFA优 ...

  4. bzoj 3126: [Usaco2013 Open]Photo——单调队列优化dp

    Description 给你一个n长度的数轴和m个区间,每个区间里有且仅有一个点,问能有多少个点 Input * Line 1: Two integers N and M. * Lines 2..M+ ...

  5. BZOJ 3126 [USACO2013 Open]Photo (单调队列优化DP)

    洛谷传送门 题目大意:给你一个长度为$n$的序列和$m$个区间,每个区间内有且仅有一个1,其它数必须是0,求整个序列中数字1最多的数量 神题,竟然是$DP$ 定义$f_{i}$表示第i位放一个1时,最 ...

  6. bzoj 3126 单调队列优化dp

    能转移的最左是其左边完整区间的最右左端点,最右是能覆盖它的最左左端点-1 #pragma GCC optimize ("O3") #include<cstdio> #i ...

  7. bzoj usaco 金组水题题解(2)

    续.....TAT这回不到50题编辑器就崩了.. 这里塞40道吧= = bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 比较经典的最小割?..然而 ...

  8. USACO 刷题记录bzoj

    bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草——背包 #include<cstdio> #include<cstring> #incl ...

  9. bzoj3126[Usaco2013 Open]Photo 单调队列优化dp

    3126: [Usaco2013 Open]Photo Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 374  Solved: 188[Submit] ...

随机推荐

  1. Apache添加到windows服务和移除Apache的windows服务

    Apache添加到windows服务和移除Apache的windows服务 Apache免安装版将其添加到Windows服务中: 打开cmd控制台,在上面输入"你的Apache安装目录\bi ...

  2. cc.Node—事件响应

    触摸事件1: 触摸事件类型: START, MOVED, ENDED(物体内), CANCEL(物体外);2: 监听触摸事件: node.on(类型, callback, target(回掉函数的th ...

  3. Vmware下的Linux系统,安装WPS报错:[Errno 256] No more mirrors to try

    最近新装了虚拟环境Vmware下的Linux系统,准备看doc文档发现不能读取,才想起来一起都是重新开始的~没别的~开始安装吧: 1.关虚拟机Linux,添加cdrom镜像ISO文件--开虚拟机--- ...

  4. response对象处理HTTP文件头(禁用缓存、设置页面自动刷新、定时跳转网页)

    response对象处理HTTP文件头 制作人:全心全意 禁用缓存 在默认情况下,浏览器将会对显示的网页内容进行缓存.这样,当用户再次访问相关网页时,浏览器会判断网页是否有变化,如果没有变化则直接显示 ...

  5. CCF201703-1 分蛋糕 java(100分)

    试题编号: 201703-1 试题名称: 分蛋糕 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明今天生日,他有n块蛋糕要分给朋友们吃,这n块蛋糕(编号为1到n)的重量分别 ...

  6. 洛谷 3285 [JLOI2014]松鼠的新家

    [题解] 给出一条路径,问树上的点被经过了几次. 显然树剖之后树上差分就好了. #include<cstdio> #include<algorithm> #define N 3 ...

  7. 洛谷 4216 BZOJ 4448 [SCOI2015]情报传递

    [题解] 每个情报员的危险值val[i]应该是一个分段函数,前面一段是平行于x轴的横线,后面一段是一次函数.我们可以用fx(t)=t-b[x]表示这个一次函数.每次询问一条链上fx(t)大于c的点的个 ...

  8. c#string类型反序列化成字典类型

    c# 实现string类型转化为字典类型:黄色底纹为需要引用的dll,可以在网站下载! 下载地址:http://download.csdn.net/download/xinping_168/47107 ...

  9. 自定义Realm

    [单Realm] 1) jar包 2) 实现自定义Realm public class RealmOne implements Realm{ /** * 获取基本类名 */ @Override pub ...

  10. git删除远程remote分支

    git 命令如下: git push origin --delete <远程分支名字>