[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=1715

[算法]

用SPFA判定负环是否存在即可

时间复杂度 : O(N ^ 2)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 500010
const int inf = 2e9; struct edge
{
int to , w , nxt;
} e[MAXN]; int n , m , p , tot;
int head[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T 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 bool spfa()
{
static int dist[MAXN] , cnt[MAXN];
static bool inq[MAXN];
queue< int > q;
for (int i = ; i <= n; i++)
{
cnt[i] = ;
dist[i] = inf;
inq[i] = false;
}
cnt[] = ;
dist[] = ;
q.push();
while (!q.empty())
{
int cur = q.front();
q.pop();
inq[cur] = false;
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (dist[cur] + w < dist[v])
{
dist[v] = dist[cur] + w;
if (!inq[v])
{
inq[v] = true;
if (++cnt[v] > n) return true;
q.push(v);
}
}
}
}
return false;
} int main()
{ int T;
read(T);
while (T--)
{
read(n); read(m); read(p);
tot = ;
for (int i = ; i <= n; i++) head[i] = ;
for (int i = ; i <= m; i++)
{
int u , v , w;
read(u); read(v); read(w);
addedge(u , v , w);
addedge(v , u , w);
}
for (int i = ; i <= p; i++)
{
int u , v , w;
read(u); read(v); read(w);
addedge(u , v , -w);
}
if (spfa()) printf("YES\n");
else printf("NO\n");
} return ; }

[USACO2006 DEC] Wormholes的更多相关文章

  1. BZOJ1715: [Usaco2006 Dec]Wormholes 虫洞

    1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 475  Solved: 263[Sub ...

  2. 1715: [Usaco2006 Dec]Wormholes 虫洞

    1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 501  Solved: 278[Sub ...

  3. bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞 -- spfa判断负环

    1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec  Memory Limit: 64 MB 注意第一次加边是双向边第二次是单向边,并且每次询问前数 ...

  4. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  5. bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】

    tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstrin ...

  6. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  7. BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]

    1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1017  Solved: ...

  8. bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式

    后缀数组+二分答案+离散化.(上次写的时候看数据小没离散化然后一直WA...写了lsj师兄的写法. #include<cstdio> #include<cstring> #in ...

  9. Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 554  Solved: 346[ ...

随机推荐

  1. Maven实战:Pom.xml详解

    什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...

  2. 王垠:完全用Linux工作 (2003)

    完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作. GNU/Linux 不是每个人都想用的.如果你只需要处理一般的事务, ...

  3. codevs——1690 开关灯

    1690 开关灯 USACO  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description YYX家门前的街上有N( ...

  4. Hadoop安装和基本单机部署

    下载安装  # 下载 $ cd /usr/local $ wget http://mirrors.hust.edu.cn/apache/hadoop/common/hadoop-2.9.2/hadoo ...

  5. IOS --关于粘贴板 ,剪切板 ,UILabel的复制

    在iOS中下面三个控件,自身就有复制-粘贴的功能: 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程 ...

  6. Activiti 5.17 实体对象与类和数据库表的映射

    一.Activiti 5.17 mybatis的mapping文件声明映射的实体对象关系. <configuration><settings><settingname=& ...

  7. iOS10获得系统权限

    iOS 10 对系统隐私权限的管理更加严格,如果你不设置就会直接崩溃,一般解决办法都是在info.plist文件添加对应的Key-Value就可以了. <!-- 相册 --> <ke ...

  8. [转]gzip,bzip2,tar,zip命令使用方法详解

    原文:http://blog.chinaunix.net/uid-20779720-id-2547669.html 1 gzipgzip(1) 是GNU的压缩程序.它只对单个文件进行压缩.基本用法如下 ...

  9. 安卓开发懒鬼最爱之ButterKnife,依赖注入第三方是库,进一步加速开发速度

    转载请注明出处:王亟亟的大牛之路 还在烦躁一大堆findById的控件操作而烦恼么? 平时,我们的那一系列findById是一个"浩大的project"样比例如以下 这是以前一个项 ...

  10. cocos2d-x改底层之获取UIListView的实际内容大小

    实际项目中UI界面中常常会用到UIListView.大多会在CocoStudio中直接加入这个控件. 可是在使用中发现了一些坑和功能缺乏,然后就看了一下底层的逻辑,发现略微改一下底层就能够满足需求,所 ...