【luogu P3385 负环】 模板
题目链接:https://www.luogu.org/problemnew/show/P3385
SPFA判负环。
这个题必须卡一卡才过得去。
按理说对于一个负环点应当是入队 > n次。
但是这个题数据不是很友好qwq
所以我们把入队次数变成 >= (n/4)次。
到考试的时候你说是写 > n 还是 > (n/4) ?
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10000 + 10;
const int inf = 0x7fffffff;
struct edge{
int from, to, next, len;
}e[maxn<<2];
int head[maxn], cnt, visnum[maxn], dis[maxn];
int vis[maxn];
queue<int> q;
int T, n, m;
void add(int u, int v, int w)
{
e[++cnt].from = u;
e[cnt].len = w;
e[cnt].next = head[u];
e[cnt].to = v;
head[u] = cnt;
}
bool SPFA()
{
while(!q.empty())
{
int now = q.front();q.pop();
vis[now] = 0;
for(int i = head[now]; i != -1; i = e[i].next)
{
if(dis[e[i].to] > dis[now] + e[i].len)
{
dis[e[i].to] = dis[now] + e[i].len;
if(!vis[e[i].to])
{
visnum[e[i].to]++;
if(visnum[e[i].to] >= (n>>2)) return 1;
q.push(e[i].to);
}
}
}
}
return 0;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
cnt = 0;
while(!q.empty()) q.pop();
memset(visnum,0,sizeof(visnum));
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(e,0,sizeof(e));
for(int i = 1; i <= n; i++) dis[i] = inf;
for(int i = 1; i <= m; i++)
{
int u, v, w;
scanf("%d%d%d",&u,&v,&w);
if(w >= 0)
{
add(u, v, w); add(v, u, w);
}
if(w < 0)
{
add(u, v, w);
}
}
int s = 1;
q.push(s); dis[s] = 0; vis[s] = 1;
if(SPFA())
printf("YE5\n");
else
printf("N0\n");
}
return 0;
}
【luogu P3385 负环】 模板的更多相关文章
- ACM - 图论 - P3385 负环
P3385 负环 题目描述 给定一个 \(n\) 个点的有向图,请求出图中是否存在从顶点 \(1\) 出发能到达的负环. 负环的定义是:一条边权之和为负数的回路. 输入格式 本题单测试点有多组测试数据 ...
- Wormholes---poj3259(最短路 spfa 判断负环 模板)
题目链接:http://poj.org/problem?id=3259 题意是问是否能通过虫洞回到过去: 虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts. 我们把虫洞看成是一条负权路,问 ...
- [luogu3385]dfs_spfa判负环模板
解题关键:模板保存. 判负环不需要memset dis数组,因为已经更新过得d数组一定小于0,如果当前点可以更新d,说明d更小,有可能继续扩大负环,所以继续更新:如果比d[v]大,则不可能继续更新负环 ...
- Luogu 3385 负环 | 我有特别的SPFA技巧
这样似乎跑得快: 初始化所有的dis是0,然后枚举每个点作为起点,用DFS更新所有点的dis: 如果更新到一个栈中节点,那么有负环. #include <cstdio> #include ...
- POJ 3259 Wormholes【最短路/SPFA判断负环模板】
农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...
- SPFA判负环模板
void DFS_SPFA(int u){ if(flag) return; vis[u]=true; for(int i=head[u];i;i=edges[i].nxt){ if(fl ...
- 洛谷P3385负环
传送门 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...
- 洛谷—— P3385 【模板】负环
题目描述 暴力枚举/SPFA/Bellman-ford/奇怪的贪心/超神搜索 输入输出格式 输入格式: 第一行一个正整数T表示数据组数,对于每组数据: 第一行两个正整数N M,表示图有N个顶点,M条边 ...
- poj3259 Wormholes (判负环)【spfa】(模板)
<题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...
随机推荐
- Hadoop Ecosytem
There are a lot of Hadoop related projects which are open sourced and widely used by many componies. ...
- Nginx图片服务器
最近总项目因为需要显示图片,就使用了Nginx,使用很简单,下面简单介绍一下怎么用 压缩包下载地址,解压即用 http://files.cnblogs.com/files/sanduweiliangx ...
- js跑步算法
<!DOCTYPE html> <html> <head> <title>JavaScript</title> <style> ...
- win10的xbox下载应用或者游戏时,出现0x80070422和0x80073D0A的解决办法
这个错误:0x80070422是因为关闭了windows update这个服务导致的 这个错误:0x80073D0A是因为关闭了windows firewall这个服务导致的 具体操作: cmd下se ...
- javascript学习:闭包和prototype原型使用基础
闭包 function Person(name) { this.Username = name; var Userage = 18; //通过这种方法可以模拟私有成员 //类似于private成员 t ...
- Git读档
$ git config --global user.name "meng kai" $ git config --global user.email 363255751@qq.c ...
- 两个command的疑惑
1.在cqrs模式中有command和query command 命令 没有返回值,但会更改对象的状态 query 查询 有返回值 但不会改变用户的状态,对下同而言没有副作用 2.在今天的实际 ...
- html-框架标签的使用
<frameset> - rows:按照行进行划分 ** <frameset rows="80,*"> - cols:按照列进行划分 ** <fram ...
- 使用 iframe + postMessage 实现跨域通信
在实际项目开发中可能会碰到在 a.com 页面中嵌套 b.com 页面,这时第一反应是使用 iframe,但是产品又提出在 a.com 中操作,b.com 中进行显示,或者相反. 1.postMess ...
- 爬楼梯C++
class Solution {public: /** * @param n: An integer * @return: An integer */ int climbStairs(int n) { ...