题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环

因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说明存在有负环

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int maxn=;
int d[maxn];
int flag;
int nodenum,edgenum,ecnt; struct Edge{
int u,v,cost;
} e[maxn]; int Bellman_ford(){
for(int i=;i<=nodenum;i++) d[i]=INF;
d[]=; for(int i=;i<nodenum;i++){
for(int j=;j<=ecnt;j++){
if(d[e[j].v]>d[e[j].u]+e[j].cost)
d[e[j].v]=d[e[j].u]+e[j].cost;
}
} flag=;
for(int i=;i<=ecnt;i++){
if(d[e[i].v]>d[e[i].u]+e[i].cost){
flag=;
break;
}
}
return flag;
} void addedges(int a,int b,int c){
e[++ecnt].u=a;
e[ecnt].v=b;
e[ecnt].cost=c;
} int main(){
int m,w,ncase;
int a,b,c;
scanf("%d",&ncase);
while(ncase--){
ecnt=;
scanf("%d %d %d",&nodenum,&m,&w);
while(m--){
scanf("%d %d %d",&a,&b,&c);
addedges(a,b,c);
addedges(b,a,c);
}
while(w--){
scanf("%d %d %d",&a,&b,&c);
c=-c;
addedges(a,b,c);
}
if(Bellman_ford()) printf("NO\n");
else printf("YES\n");
}
return ;
}

POJ 3259 Wormholes【Bellman_ford判断负环】的更多相关文章

  1. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  2. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  3. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  4. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  5. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  6. POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】

    链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  7. POJ 3259:Wormholes bellman_ford判定负环

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37906   Accepted: 13954 Descr ...

  8. poj 3259 (Bellman_Ford判断负环)

    题意:John的农场里n块地,m条路连接两块地,k个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己. 思路:虫洞 ...

  9. Wormholes POJ 3259(SPFA判负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

随机推荐

  1. 17+个ASP.NET MVC扩展点,含源码{转}

    1.自定义一个HttpModule,并将其中的方法添加到HttpApplication相应的事件中!即:创建一个实现了IHttpmodule接口的类,并将配置WebConfig.在自定义的HttpMo ...

  2. Sqli-labs less 27

    Less-27 本关主要考察将union,select和26关过滤掉的字符.此处我们依旧和26关的方式是一样的,只需要将union和select改为大小写混合就可以突破. 示例:127.0.0.1/s ...

  3. Linux Rsync

    一.Rsync介绍 1.什么是Rsync Rsync 即Remote Rynchronization,是一款开源的.快速的.多功能的.可实现全量或增量的本地或者远程数据镜像同步复制.备份的优秀工具. ...

  4. uva 11324

    Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First ...

  5. linux 下各个工具使用(screen、tmux,pyenv、virtualenv,pip国内源,tree)

    一.多会话工具screen.tmux 两个都是多窗口工具.1.使用后wim出现配色问题:http://ibartman.com/2014/04/16/vim%20%E9%85%8D%E8%89%B2/ ...

  6. PCA和LDA

    一.PCA     在讲PCA之前,首先有人要问了,为什么我们要使用PCA,PCA到底是干什么的?这里先做一个小小的解释,举个例子:在人脸识别工作中一张人脸图像是60*60=3600维,要处理这样的数 ...

  7. 深入了解linux下的last命令及其数据源

    http://www.9usb.net/200902/linux-last.html http://blog.csdn.net/chaofanwei/article/details/11826567

  8. Jenkins+Maven+Git CI环境搭建手册

    Jenkins+Maven+Git CI环境搭建手册 环境: OS:Linux version 2.6.32-220.23.2.ali878.el6.x86_64 (ads@kbuild) (gcc ...

  9. 用httpPost对JSON发送和接收的例子

    HTTPPost发送JSON: private static final String APPLICATION_JSON = "application/json";         ...

  10. Js 日期操作

    Js获取当前日期时间及其它操作var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    ...