[题目链接]

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. HTTP请求的缓存(Cache)机制

    原文地址:http://small.aiweimeng.top/index.php/archives/58.html 先来一张图: ####下面简单的来描述一下HTTP Cache机制: 当资源资源第 ...

  2. python的__name__和dir()属性

    1.__name__属性 一个模块被另一个程序第一次引入时,其主程序将运行.如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身运行时执行.示例 ...

  3. 多个Nginx如何实现集群(没具体方案,只是初步探究)

    场景: Nginx+Web服务器可以实现负载均衡,但是一台Nginx也是有限的,如果并非量高的话,在他的上层如何实现负载均衡. 如果是DNS或者CDN的话,建多个机房,势必有多个机房数据同步的问题. ...

  4. gdb生成的core文件位置

    gdb可以生成core文件,记录堆栈信息,core文件名字是下面这种格式 :core.9488,其中9488是PID 文件位置是当前目录

  5. 深入理解javascript之设计模式

    设计模式 设计模式是命名.抽象和识别对可重用的面向对象设计实用的的通用设计结构. 设计模式确定类和他们的实体.他们的角色和协作.还有他们的责任分配. 每个设计模式都聚焦于一个面向对象的设计难题或问题. ...

  6. c++中c_str()函数

    https://zhidao.baidu.com/question/104592558.html

  7. SolidEdge如何绘制变化半径倒圆角

    1 在要变化半径的边上打一些点   2 点击倒角命令的参数对话框,选择可变半径   3 选择倒角的直线,右击确认,再依次点击关键点,修改倒角数值,修改之后按回车,继续下一个点,直到结束.  

  8. VS2012,VS2010无法生成dll程序集的解决办法

    在我们做项目的时候总会遇到dll程序集无法生成导致各种问题. 通常我们的做法就是清理项目,然后重新生成,或者直接到bin目录下删除所有dll,然后重新生成. 有时候某几个dll就是不生成. 这时候就需 ...

  9. [java][db]JAVA分布式事务原理及应用

    JTA(Java Transaction API)同意应用程序运行分布式事务处理--在两个或多个网络计算机资源上訪问而且更新数据.JDBC驱动程序的JTA支持极大地增强了数据訪问能力.  本文的目的是 ...

  10. linux下提示command not found

    首先就要考虑root 的$PATH里是否已经包含了这些环境变量. 主要是这四个:/bin ,/usr/bin,/sbin,/usr/sbin. 四个主要存放的东东: ./bin: bin为binary ...