[USACO2006 DEC] Wormholes
[题目链接]
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的更多相关文章
- BZOJ1715: [Usaco2006 Dec]Wormholes 虫洞
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 475 Solved: 263[Sub ...
- 1715: [Usaco2006 Dec]Wormholes 虫洞
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 501 Solved: 278[Sub ...
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞 -- spfa判断负环
1715: [Usaco2006 Dec]Wormholes 虫洞 Time Limit: 5 Sec Memory Limit: 64 MB 注意第一次加边是双向边第二次是单向边,并且每次询问前数 ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】
tag是假的,用了及其诡异的方法判负环 正权无向边和负权有向边的图 #include<iostream> #include<cstdio> #include<cstrin ...
- BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环
Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...
- BZOJ 1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 [后缀数组]
1717: [Usaco2006 Dec]Milk Patterns 产奶的模式 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1017 Solved: ...
- bzoj1717: [Usaco2006 Dec]Milk Patterns 产奶的模式
后缀数组+二分答案+离散化.(上次写的时候看数据小没离散化然后一直WA...写了lsj师兄的写法. #include<cstdio> #include<cstring> #in ...
- Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 554 Solved: 346[ ...
随机推荐
- Peter Norvig:十年学会编程
为啥都想速成? 随便逛一下书店,你会看到<7天自学Java>等诸如此类的N天甚至N小时学习Visual Basic.Windows.Internet的书.我用亚马逊网站的搜索功能,出版年份 ...
- seo优化入门教程:认识搜索引擎
对于从来没有学过seo或者零基础的人来说,搜索引擎可能都不太了解.所以我们先来认识搜索引擎有哪些,同时为什么我们要学习搜索引擎优化. 从目前全球的一个搜索引擎来说的话,他的分支是非常多的,甚至可以讲, ...
- 使用datatables实现列宽设置、水平滚动条、显示某列部分内容
示例 1.//使用 columnDefs 给列设置宽度 $('#example').DataTable( { "columnDefs": [ //给第一列指定宽度为表格整个宽度的2 ...
- 【c++】面向对象程序设计之关于继承
面向对象程序设计的核心思想是数据抽象(类的接口与实现分离).继承和动态绑定 基类 虚函数:基类希望派生类各自定义适合自身的版本的函数 在c++中,当我们使用基类的引用或指针调用虚函数时将发生动态绑定. ...
- [转]c中按位分配int的方法
从网上看到这样一段c代码,让我发觉我的C基本功还是不行啊~~ typedef struct xp { int a:2; int b:2; unsigned int c:1; } xp; 不知道大家对i ...
- reorder-list——链表、快慢指针、逆转链表、链表合并
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- HDU 5478 Can you find it
Can you find it Time Limit: 8000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- v-bind指令
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- OpenStack源码系列---neutron-server
在看过了nova模块的源码之后,再去看OpenStack其它模块的源码会轻松很多,因为框架也是大同小异的.自四月份开通博客写了几篇文章后,真心觉得写篇技术文章如果要把前前后后牵扯到的其它技术内容都做介 ...
- (转)我在北京工作这几年 – 一个软件工程师的反省
我于2007年来到北京,在北京工作这些年,先后在NEC.风行.百度几家公司担任软件工程师的职务.NEC是一家具有百年历史的传统日企,在知春路的分公司叫日电电子,我们部门主要从事机顶盒.数字电视上嵌入式 ...