[P2850][USACO06DEC]虫洞Wormholes (最短路)
死活调不出来
后来是发现这题建边的原因……
吐血.jpg
所谓的虫洞传说也就是负边了
然后这里打的spfa和原来的不一样
感觉hzwer大佬的spfa好强啊……
也更易写一点
贴代码
#include<bits/stdc++.h>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define ll long long
#define N 100005
#define INF 2147483647
using namespace std;
inline int read() {
int f = , x = ; char ch;
do { ch = getchar(); if (ch == '-')f = -; } while (ch<'' || ch>'');
do { x = x * + ch - ''; ch = getchar(); } while (ch >= ''&&ch <= '');
return f * x;
}
bool flag;
int n,m,w,cnt;
int head[],dis[];
bool mark[];
struct data{
int to,next,v;
}e[];
void add(int u,int v,int w)
{
e[++cnt].to=v;e[cnt].next=head[u];head[u]=cnt;e[cnt].v=w;
}
void spfa(int x)
{
mark[x]=;
for(int i=head[x];i;i=e[i].next)
if(e[i].v+dis[x]<dis[e[i].to])
{
if(mark[e[i].to]){flag=;return;}
else
{
dis[e[i].to]=e[i].v+dis[x];
spfa(e[i].to);
}
}
mark[x]=;
}
bool check()
{
for(int i=;i<=n;i++){
dis[i]=INF;
mark[i]=;
}
flag=;
for(int i=;i<=n;i++)
{
dis[i]=;
spfa(i);
if(flag) return ;
}
return ;
}
int main()
{
int F=read();
while(F--)
{
cnt=;
n=read(),m=read(),w=read();
memset(head,,sizeof(head));
for(int i=;i<=m;i++)
{
int s=read(),e=read(),t=read();
add(s,e,t);
add(e,s,t);
}
for(int i=;i<=w;i++)
{
int s=read(),e=read(),t=read();
add(s,e,-t);
}
if(check()) puts("YES");
else puts("NO");
}
return ;
}
[P2850][USACO06DEC]虫洞Wormholes (最短路)的更多相关文章
- 洛谷 P2850 [USACO06DEC]虫洞Wormholes 题解
P2850 [USACO06DEC]虫洞Wormholes 题目描述 While exploring his many farms, Farmer John has discovered a numb ...
- 洛谷 P2850 [USACO06DEC]虫洞Wormholes 判负环
虫洞(wormhole) FJ 在农场上闲逛时,发现他的农场里有很多虫洞.虫洞是一条特殊的有向路径,当 FJ 从它的一头走到另一头后,他将被传送到过去的某个时刻.FJ 的每个农场包括 N(1<= ...
- Wormholes 最短路判断有无负权值
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ 3259 Wormholes (最短路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34302 Accepted: 12520 Descr ...
- poj 3259 Wormholes(最短路 Bellman)
题目:http://poj.org/problem?id=3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ...
- POJ 3259 Wormholes 最短路+负环
原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...
- 洛谷2850 【Usaco2006 Dec】虫洞Wormholes SPFA
问题描述 John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N (从1..N ...
- 2019寒假练题计划——LibreOJ刷题计划 &《信息学奥赛一本通》提高版题目
目录 2019.1.27 #10082. 「一本通 3.3 例 1」Word Rings 题意 思路 #10083. 「一本通 3.3 例 2」双调路径 题意 思路 #10084. 「一本通 3.3 ...
- LOJ 一本通一句话题解系列:
第一部分 基础算法 第 1 章 贪心算法 1):「一本通 1.1 例 1」活动安排:按照结束时间排序,然后扫一遍就可以了. 2):「一本通 1.1 例 2」种树:首先要尽量的往区间重叠的部分种树,先按 ...
随机推荐
- 点击图片弹出input type=file选择器
<label for="UploadCoverPhoto" class="cursor-pointer"> <img class=" ...
- C++11 中的function和bind、lambda用法
std::function 1. std::bind绑定一个成员函数 #include <iostream> #include <functional> struct Foo ...
- Caused by: java.lang.ClassNotFoundException: backtype.storm.topology.IRichSpout
1:初次运行Strom程序出现如下所示的错误,贴一下,方便脑补,也希望帮助到看到的小伙伴: 错误如下所示,主要问题是刚开始使用maven获取jar包的时候需要写<scope>provide ...
- 旋转矩阵 The Rotation Matrix
参考: http://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/how-does ...
- 使用 curses 函数库管理基于文本的屏幕
curses 函数库提供了终端无关的方式来编写全屏幕的基于字符的程序.curses 还可以管理键盘,提供了一种简单易用的非阻塞字符输入模式. curses 函数库能够优化光标的移动并最小化需要对屏幕进 ...
- 令人疑惑的 std::remove 算法
摘自<Effective STL>第32条 remove的声明: template<class ForwardIterator, class T> ForwardIterato ...
- bzoj 2832
题解: 首先有一个比较显然的事情是如果我们确定了买的次数这道题就可以简单的贪心了 但是答案和买的次数是什么关系呢.. 好像是可以三分的 所以应该是单峰的 这里用了模拟退火,而且是没有处理失败情况的模拟 ...
- 【转载】DDD分层架构的三种模式
引言 在讨论DDD分层架构的模式之前,我们先一起回顾一下DDD和分层架构的相关知识. DDD DDD(Domain Driven Design,领域驱动设计)作为一种软件开发方法,它可以帮助我们设计高 ...
- BZOJ2219 数论之神 数论 中国剩余定理 原根 BSGS
原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2219.html 题目传送门 - BZOJ2219 题意 求同余方程 $x^A\equiv B \pmo ...
- sentinel-dashboard安装、运行(ubuntu)
下载页面https://github.com/alibaba/Sentinel/releases wget -P /opt/downloads https://github.com/alibaba/S ...