tag是假的,用了及其诡异的方法判负环

正权无向边和负权有向边的图

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=505,inf=210000000;
int n,m,w,h[N],cnt,d[N];
struct qwe
{
int ne,to,va;
}e[N*N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v,int w)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
e[cnt].va=w;
h[u]=cnt;
}
bool ok()
{
for(int i=1;i<=n;i++)
d[i]=inf;
d[1]=0;
for(int i=1;i<n;i++)
for(int j=1;j<=n;j++)
for(int k=h[j];k;k=e[k].ne)
if(d[e[k].to]>e[k].va+d[j])
d[e[k].to]=e[k].va+d[j];
for(int i=1;i<=n;i++)
for(int j=h[i];j;j=e[j].ne)
if(d[e[j].to]>d[i]+e[j].va)
return 1;
return 0;
}
int main()
{
int T=read();
while(T--)
{
memset(h,0,sizeof(h));
cnt=0;
n=read(),m=read(),w=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read(),z=read();
add(x,y,z),add(y,x,z);
}
for(int i=1;i<=w;i++)
{
int x=read(),y=read(),z=read();
add(x,y,-z);
}
if(ok())
puts("YES");
else
puts("NO");
}
return 0;
}

bzoj 1715: [Usaco2006 Dec]Wormholes 虫洞【spfa判负环】的更多相关文章

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

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

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

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

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

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

  4. 1715: [Usaco2006 Dec]Wormholes 虫洞

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

  5. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  6. POJ3259 Wormholes 【spfa判负环】

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  7. POJ3259:Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 68097   Accepted: 25374 题目链接: ...

  8. UVA 558 Wormholes 【SPFA 判负环】

    题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...

  9. Wormholes(spfa判负环)

      POJ - 3259—— Wormholes Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & % ...

随机推荐

  1. 关于zkfc与zkserver频繁断开的问题

    详见http://blog.csdn.net/dslztx/article/details/51596951

  2. 【Codeforces 1086B】Minimum Diameter Tree

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 统计叶子节点个数m 把每条和叶子节点相邻的边权设置成s/cnt就可以了 这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶 ...

  3. 九度oj 题目1490:字符串链接

    题目1490:字符串链接 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:2610 解决:1321 题目描述: 不用strcat 函数,自己编写一个字符串链接函数MyStrcat(char ...

  4. noip模拟赛 轮换

    分析:模拟题,关键就是要理解题目意思.m≥3的轮换可以拆成m=2的小轮换,小轮换的话只需要交换一下就可以了. #include <cstdio> #include <cstring& ...

  5. java中使用Protobuf的实例(Demo)

    由于Protobuf受到推崇,故尝试采用protobuf来摒弃传统的xml进行传输数据. 首先,需要下载的关于Protobuf的文件: 1.到http://code.google.com/p/prot ...

  6. Codeforces 631B Print Check【模拟】

    题意: 按顺序给定列和行进行涂色,输出最终得到的方格颜色分布. 分析: 记录下涂的次序,如果某个元素的横和列都被涂过,那么就选择次序最大的颜色. 代码: #include<iostream> ...

  7. [bzoj3513][MUTC2013]idiots_FFT

    idiots bzoj-3513 MUTC-2013 题目大意:给定$n$根木棍,问随机选择三根能构成三角形的概率. 注释:$1\le n\le 3\cdot 10^5$,$1\le a_i\le 1 ...

  8. hdu4701 Game(递推博弈)

    题意: Alice初始有A元,Bob有B元. 有N个物品,第i个物品价值为Ci.Alice和Bob轮流买一些(>=1)物品.不能移动的人输.购买有一个限制,对于第1 个之后物品,只有当第i-1个 ...

  9. JS中 为什么很多要用两个!! 来判断

    比如 if(!!last) 这个就表示 if(last || false).将判断的类型,强转成boolean类型.如果last是null(或者undefine)的话,!last,返回的就是true ...

  10. 我的arcgis培训照片4 来自http://www.cioiot.com/successview-549-1.html